import re from collections import Counter def main(): N,M=map(int,input().split()) assert(0<=N<=50000) assert(1<=M<=50000) d=Counter() S_set=set() T_set=set() for i in range(N): S,A=input().split() assert(1<=int(A)<=9999) assert(re.fullmatch("^\w+$",S)!=None) assert(2<=len(S)<=16) S_set.add(S) d[S]=int(A) assert(len(S_set)==N) for i in range(M): T,B=input().split() assert(1<=int(B)<=9999) assert(re.fullmatch("^\w+$",T)!=None) assert(2<=len(T)<=16) assert(d[T]<=int(B)) T_set.add(T) d[T]=int(B) assert(len(T_set)==M) ans=sorted(list(d.items()),key=lambda x:x[0]) for user,rate in ans: print(user,rate) return main()