N,M = map(int,input().split()) dic = {} for i in range(N): S,A = input().split() A = int(A) dic[S] = A for i in range(M): S,A = input().split() A = int(A) dic[S] = A SA = [] for i in dic: SA.append( (i,dic[i]) ) SA.sort() for i in SA: print (*i)