n = int(eval(input()))

o = {}

for i in range(n):

    p, m = input().split()

    o[p] = o.get(p, 0) + int(m)

k = list(o.keys())

k = [(len(x), x) for x in k]

k.sort()

for i in k:

    print((i[1], o[i[1]]))
o={}

for _ in range(int(eval(input()))):p,m=input().split();o[p]=o.get(p,0)+int(m)

k=sorted([(len(x),x) for x in list(o.keys())])

for _,i in k:print((i,o[i]))
n = int(eval(input()))
o = {}
for i in range(n):
    p, m = input().split()
    o[p] = o.get(p, 0) + int(m)
k = list(o.keys())
k = [(len(x), x) for x in k]
k.sort()
for i in k:
    print((i[1], o[i[1]]))

o = {}
for _ in range(int(eval(input()))):
    p, m = input().split()
    o[p] = o.get(p, 0) + int(m)
k = sorted([(len(x), x) for x in list(o.keys())])
for _, i in k:
    print((i, o[i]))

-n = int(eval(input())) -for i in range(n): +for _ in range(int(eval(input()))): -k = list(o.keys()) -k = [(len(x), x) for x in k] -k.sort() -for i in k: -    print((i[1], o[i[1]])) +k = sorted([(len(x), x) for x in list(o.keys())]) +for _, i in k: +    print((i, o[i]))