#include<bits/stdc++.h>
using namespace std;
struct Node
{
int in;
int cn;
Node *next;
};
void init(Node **);
void create(Node *,int);
void calculate(Node *,Node *,Node *);
void print(Node *);
int main()
{
int n,m;
Node *head1,*head2,*head3;
init(&head1);
init(&head2);
init(&head3);
head3->in=2147483647;
scanf("%d",&n);
create(head1,n);
scanf("%d",&m);
create(head2,m);
calculate(head1,head2,head3);
print(head3);
return 0;
}
void init(Node **ptr)
{
*ptr=(Node*)malloc(sizeof(Node));
(*ptr)->next=NULL;
}
void create(Node *ptr,int x)
{
for(int i=1;i<=x;i++)
{
ptr->next=(Node*)malloc(sizeof(Node));
ptr=ptr->next;
scanf("%d%d",&ptr->cn,&ptr->in);
ptr->next=NULL;
}
}
void calculate(Node *ptrA,Node *ptrB,Node *ptrC)
{
int a,b;
Node *p1,*p2,*p3,*p;
p1=ptrA->next;
while(p1!=NULL)
{
p2=ptrB->next;
while(p2!=NULL)
{
a=p1->cn*p2->cn;
b=p1->in+p2->in;
p3=ptrC;
p=ptrC;
while(p3!=NULL)
{
if(b<p3->in)
{
if(p3->next==NULL)
{
p3->next=(Node*)malloc(sizeof(Node));
p3=p3->next;
p3->cn=a;
p3->in=b;
p3->next=NULL;
break;
}
else p3=p3->next;
}
else if(b==p3->in)
{
p3->cn+=a;
break;
}
else
{
p->next=(Node*)malloc(sizeof(Node));
p=p->next;
p->cn=a;
p->in=b;
p->next=p3;
break;
}
}
p2=p2->next;
}
p1=p1->next;
}
}
void print(Node *ptr)
{
int sum=0;
Node *p;
p=ptr->next;
while(p!=NULL)
{
if(p->cn!=0) sum++;
p=p->next;
}
printf("%d\n",sum);
p=ptr->next;
while(p!=NULL)
{
if(p->cn!=0) printf("%d %d\n",p->cn,p->in);
p=p->next;
}
}