import sys input = sys.stdin.readline N,M=map(int,input().split()) D=dict() for i in range(N): S,A=input().split() A=int(A) D[S]=A for i in range(M): S,A=input().split() A=int(A) D[S]=A L=sorted(D) for x in L: print(x,D[x])