from collections import defaultdict d=defaultdict(str) n,m=map(int,input().split()) for _ in range(n): s,t=map(str,input().split()) d[s]=t for _ in range(m): s,t=map(str,input().split()) d[s]=t ans=[] for key,val in d.items(): ans.append([key,val]) ans.sort() for a,b in ans: print(a,b)