#include <fstream>
#include <iostream>
#include <vector>
#include <map>
#include <stdlib.h>

using namespace std;

int main() {

	ifstream input("DATA3.txt");
	ofstream output("OUT3.txt");
	
	string temp;
	vector <string> lines;
	while (getline(input, temp)) if (!temp.empty()) lines.push_back(temp);
	
	int i = 0;
	while (i < lines.size()) {
	
		int numBills = atoi(lines[i].c_str());
		int numAmendments = atoi(lines[i + numBills + 1].c_str());
		
		vector <string> bills;
		vector <string> amendments;
		
		for (int x = i + 1; x < (i + 1 + numBills); ++x) {
			bills.push_back(lines[x]);
		}
		
		for (int x = i + numBills + 2; x < (i + numBills + numAmendments + 2); ++x) {
			amendments.push_back(lines[x]);
		}
		
		map <string, int> balances;
		map <string, int> owe;
		
		for (int x = 0; x < amendments.size(); ++x) {
			size_t space = amendments[x].find(" ");
			string id = amendments[x].substr(0, space);
			int owing = atoi(amendments[x].substr(space + 1, amendments[x].size() - space + 1).c_str());
			owe[id] += owing;
		}
		
		
		for (int x = 0; x < bills.size(); ++x) {
			size_t space = bills[x].find(" ");
			string id = bills[x].substr(0, space);
			int bill = atoi(bills[x].substr(space + 1, bills[x].size() - space + 1).c_str());
			int prevAmount = owe[id];
			owe[id] = owe[id] - bill;
			if (owe[id] > 0) {
				balances[id] = 0;
			} else {
				balances[id] = bill - prevAmount;
			}
			
			output << id << " " << balances[id] << endl;
		}

		i += numBills + numAmendments + 2;
		
	}

}


