# include<stdlib.h>

//链表实现
struct node{
int cofe;
int expn;
struct node next;
};
typedef struct node
Node;
Node Creatlist()//带头结点
{
}
{
Node Newp;
Newp=(Node)malloc(sizeof(struct node));
Newp->cofe=cofe;
Newp->expn=expn;
Newp->next=NULL;
rear->next=Newp;
rear=Newp;
}
{
{
}
}
{
{
}
}
{
Node List,rear,Newp;
List=(Node)malloc(sizeof(struct node));
List->next=NULL;
{
Newp=(Node)malloc(sizeof(struct node));

	if(head1->expn>head2->expn)
{
}
{
}
else
{
}
if(Newp->cofe){
Newp->next=NULL;
rear=Findrear(List);
rear->next=Newp;
rear=Newp;
}
else free(Newp);//系数和为0；
}
{
Newp=(Node)malloc(sizeof(struct node));
Newp->next=NULL;

rear=Findrear(List);
rear->next=Newp;
rear=Newp;

}
{
Newp=(Node)malloc(sizeof(struct node));
Newp->next=NULL;

rear=Findrear(List);
rear->next=Newp;
rear=Newp;

}

return List;


}
int main()
{

head1=Creatlist();

int n,m,i,cofe,expn;//n,m分别是项的个数

scanf("%d",&n);
for(i=0;i<n;i++)
{
scanf("%d %d",&cofe,&expn);
}
getchar();
scanf("%d",&m);
for(i=0;i<m;i++)
{
scanf("%d %d",&cofe,&expn);