n,m=map(int,input().split()) d={} for i in range(n): a,b=input().split() d[a]=int(b) for i in range(m): a,b=input().split() d[a]=int(b) l=sorted(d) for x in l: print(x,d[x])