#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>
#include <algorithm>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <ctime>

#define forr(a,b,c) for(int (a) = (b); (a) < (c); ++ (a))
#define fore(a,b,c) for(int (a) = (b); (a) <= (c); ++ (a))

#define pii pair<int,int>
#define int3 pair<pair<int,int>, int>
#define vi vector<int>
#define ull long long
#define vs vector<string>

//#define DEBUG

using namespace std;


int main(){
	
	#ifndef DEBUG
		freopen("DATA3.txt", "r", stdin);
		freopen("OUT3.txt", "w", stdout);
	#endif
	
	forr(_r,0,5){
		int n, t; cin>>n;
		vector<pair<string,int> > mm, nn;
		map<string, int> h;
		map<int,string> r;
		
		int pos=1;
		
		forr(_a,0,n){
			string u; cin>>u;
			if(!h[u]){
				r[pos]=u;
				h[u]=pos++;
			}
			int t; cin>>t;
			
			nn.push_back(make_pair(u,t));
		}
		
		int m; cin>>m;
		forr(_a,0,m){
			string u; cin>>u;
			if(!h[u]){
				r[pos]=u;
				h[u]=pos++;
			}
			int t; cin>>t;
			
			mm.push_back(make_pair(u,t));
		}
		
		forr(i,1,pos){
			vector<int> v;
			
			int debt=0;
			forr(j,0,mm.size()) if(mm[j].first==r[i]) debt -= mm[j].second;
			forr(j,0,nn.size()) if(nn[j].first==r[i]) v.push_back(nn[j].second);
			
			bool pr=0;
			forr(j,0,v.size()){
				debt += v[j];
				if(debt<0){
					if(r[i]!=""){
						cout << r[i] << " 0\n";
						pr=1;
					}
				}
			}
			if(!(pr && debt==0)) cout << r[i] << ' ' << debt << '\n';
		}
	}
	
	#ifdef DEBUG
		system("pause");
	#endif
	
	return 0;
}

