#include <iostream>
#include <cmath>
#include <string>
#include <vector>

using namespace std;



main()
{
    freopen("DATA3.txt", "r", stdin);
    freopen("OUT3.txt", "w", stdout);

    int a, b, k;
    string t;

    for (int x = 0; x < 5; x++)
    {
        vector <string> name1, name2;
        vector <int> val1, val2;

        cin >> a;
        for (int i = 0; i < a; i++)
        {
            cin >> t >> k;
            name1.push_back(t);
            val1.push_back(k);
        }
        cin >> b;
        for (int i = 0; i < b; i++)
        {
            cin >> t >> k;
            name2.push_back(t);
            val2.push_back(k);
        }

        for (int i = 0; i < name2.size(); i++)
        {
            for (int j = 0; j < name1.size(); j++)
            {
                if (name2[i] == name1[j])
                {
                    if (val1[j] > 0)
                    {
                        if (val2[i] > val1[j])
                            val2[i] -= val1[j], val1[j] = 0;
                        else
                        {
                            val1[j] -= val2[i];
                            val2[i] = 0;
                        }
                    }
                }
            }
        }

        for (int i = 0; i < name1.size(); i++)
        {
            cout << name1[i] << " " << val1[i] << endl;
        }
    }
    return 0;
}

