N,M = map(int,input().split()) from collections import defaultdict d = defaultdict(int) for _ in range(N+M): S,t = input().split() d[S] = int(t) l = list(d.items()) #print(l) l.sort() for S,i in l: print(S,i)