#include <iostream>
#include <fstream>
#include <string>
#include <cmath>
#include <map>
using namespace std;

int main(){
	ifstream fin("DATA3.txt");
	ofstream fout("OUT3.txt");
	pair<string,int> B[50];
	pair<string,int> A[50];
	int N, M, v;
	string ID;
	for(int T=0; T<5; T++){
		fin>>N;
		for(int n=0; n<N; n++){
			fin>>ID>>v;
			B[n] = make_pair(ID,v);
		}
		fin>>M;
		for(int m=0; m<M; m++){
			fin>>ID>>v;
			A[m] = make_pair(ID,v);
		}
		for(int i=0; i<N; i++){
			for(int j=0; j<M; j++){
				if(B[i].first == A[j].first){
					if(A[j].second > B[i].second){
						A[j].second -= B[i].second;
						B[i].second = 0;
					}
					else{ 
						B[i].second -= A[j].second;
						A[j].second = 0;
					}
				}
			}
		}
		for(int i=0; i<N; i++){
				fout<<B[i].first<<' '<<B[i].second<<endl;
		}
	}
	return 0;
}
		

