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 = d.items() l.sort() for S,i in l: print(S,i)