from collections import defaultdict import sys readline=sys.stdin.readline write=sys.stdout.write import itertools N,M=map(int,readline().split()) rate={} for n in range(N): S,A=readline().split() A=int(A) rate[S]=A for m in range(M): T,B=readline().split() B=int(B) rate[T]=B for S in sorted(list(rate.keys())): print(S,rate[S])