l={}

for i in [1]*eval(input()):

	a,n=list(map(str,input().split()))

	n=int(n)

	try:l[a]+=n

	except:l[a]=n

for i in sorted(sorted(l.keys()),key=len):print(i,l[i])
l={}

for i in [1]*eval(input()):

	a,n=input().split()

	n=int(n)

	try:l[a]+=n

	except:l[a]=n

for i in sorted(sorted(l.keys()),key=len):print(i,l[i])
l = {}
for i in [1] * eval(input()):
    a, n = list(map(str, input().split()))
    n = int(n)
    try:
        l[a] += n
    except:
        l[a] = n
for i in sorted(sorted(l.keys()), key=len):
    print(i, l[i])

l = {}
for i in [1] * eval(input()):
    a, n = input().split()
    n = int(n)
    try:
        l[a] += n
    except:
        l[a] = n
for i in sorted(sorted(l.keys()), key=len):
    print(i, l[i])

-    a, n = list(map(str, input().split())) +    a, n = input().split()