import java.io.*;
public class P3
{
    public static void main (String[] args) throws IOException
    {
	BufferedReader in = new BufferedReader (new FileReader ("DATA3.txt"));
	PrintWriter out = new PrintWriter (new FileWriter ("OUT3.txt"));
	for (int z = 0 ; z < 5 ; z++)
	{
	    int N = Integer.parseInt (in.readLine ());
	    String ns1[] = new String [N];
	    int ns2[] = new int [N];
	    String temp[];
	    for (int x = 0 ; x < N ; x++)
	    {
		temp = in.readLine ().split (" ");
		ns1 [x] = temp [0];
		ns2 [x] = Integer.parseInt (temp [1]);
	    }

	    int M = Integer.parseInt (in.readLine ());
	    String ms1[] = new String [M];
	    int ms2[] = new int [M];
	    for (int x = 0 ; x < M ; x++)
	    {
		temp = in.readLine ().split (" ");
		ms1 [x] = temp [0];
		ms2 [x] = Integer.parseInt (temp [1]);
	    }

	    for (int x = 0 ; x < M ; x++)
	    {
		for (int y = 0 ; y < M ; y++)
		{
		    if (x != y && ms1 [y].equals (ms1 [x]))
		    {
		      //  System.out.println (ms2 [x] + " " + ms2 [y]);
			ms2 [x] += ms2 [y];
			ms1 [y] = "";
			ms2 [y] = 0;
		    }
		}
	    }


	    for (int x = 0 ; x < N ; x++)
	    {
		for (int y = 0 ; y < M ; y++)
		{
		    if (ms1 [y].equals (ns1 [x]))
		    {
		      //  System.out.println (ms2 [y]);
		       // System.out.println (ns2 [x]);
		       // System.out.println ();
			//ns2 [x] -= ms2 [y];
			if (ns2 [x] - ms2 [y] <= 0)
			{

			    ms2 [y] = ms2 [y] - ns2 [x];
			    ns2 [x] = 0;
			    //   out.println (ns1 [x] + " " + ns2 [x]);

			}
			else
			{
			    ns2 [x] -= ms2 [y];
			}
		    }
		}
	    }

	    for (int x = 0 ; x < N ; x++)
	    {
		out.println (ns1 [x] + " " + ns2 [x]);
	    }

	}
	in.close ();
	out.close ();
    }
}

