#include
using namespace std;
typedef int ElementType;
typedef struct Node *PtrToNode;
struct Node {
ElementType Ratio;
ElementType Index;
PtrToNode Next;
};
typedef PtrToNode List;
List read(void ){
List head,p1,p2;
int n;
cin>>n;
head=new Node;
if(n==0)
return NULL;
p1=p2=new Node;
cin>>p1->Ratio>>p1->Index;
head->Next=p1;
for(int i=0;i<n-1;i++){
p1=new Node;
p2->Next=p1;
p2=p1;
cin>>p1->Ratio>>p1->Index;
}
p1->Next=NULL;
return head;
}
void print(List L){
if(L==NULL){
cout<<'0'<<' '<<'0'<
return ;
}
List p=L->Next;
int flag=1;
do{
if(flag)
flag=0;
else
cout<<' ';
cout<Ratio<<' '<Index;
} while(p=p->Next);
cout<
}
List Lplus(List L1,List L2){
if(!L1&&!L2)
return NULL;
if(!L1&&L2)
return L2;
if(L1&&!L2)
return L1;
List head,p1=L1->Next,p2=L2->Next,p3,p4;
head=new Node;
p4=p3=new Node;
head->Next=p3;
p3->Index=0,p3->Ratio=0;
while(p1&&p2){
if(p1->Index==p2->Index){
/* int sum=p1->Ratio+p2->Ratio;
if(sum){
p3->Index=p1->Index;
p3->Ratio=sum;
p1=p1->Next;
p2=p2->Next;
}*/
if((p1->Ratio+p2->Ratio)==0){
p1=p1->Next;
p2=p2->Next;
continue;
}
p3->Ratio=p1->Ratio+p2->Ratio;
p3->Index=p1->Index;
p1=p1->Next;
p2=p2->Next;
}
else if(p1->Index>p2->Index){
p3->Ratio=p1->Ratio,p3->Index=p1->Index;
p1=p1->Next;
}
else{
p3->Ratio=p2->Ratio,p3->Index=p2->Index;
p2=p2->Next;
}
if(p1&&p2){
p3=new Node;
p4->Next=p3;
p4=p3;
}
}
while(p1){
p3=new Node;
p4->Next=p3;
p4=p3;
p3->Ratio=p1->Ratio,p3->Index=p1->Index;
p1=p1->Next;
}
while(p2){
p3=new Node;
p4->Next=p3;
p4=p3;
p3->Ratio=p2->Ratio,p3->Index=p2->Index;
p2=p2->Next;
}
p3->Next=NULL;
return head;
}
List mul(List p1,List p2){
List head=new Node,p4,p3;
p4=p3=new Node;
head->Next=p3;
while(p2){
p3->Index=p1->Index+p2->Index;
p3->Ratio=p1->Ratio*p2->Ratio;
p2=p2->Next;
if(p2){
p3=new Node;
p4->Next=p3;
p4=p3;
}
}
p3->Next=NULL;
return head;
}
List multi(List L1,List L2){
if(!L1||!L2)
return NULL;
List p1=L1->Next,p2=L2->Next,p3;
p3= mul(p1,p2);
p1=p1->Next;
while(p1){
p3=Lplus(p3,mul(p1,p2));
p1=p1->Next;
}
return p3;
}
int main(){
List L1,L2,L3;
L1=read();
L2=read();
L3=multi(L1,L2);
print(L3);
L3=Lplus(L1,L2);
print(L3);
return 0;
}