n,m=map(int,input().split()) l=[] l2=[] for i in range(n): a,b=map(str,input().split()) b=int(b) l2.append(a) l.append([a,b]) for j in range(m): a,b=map(str,input().split()) b=int(b) if a in l2: x=l2.index(a) l[x][1]=b else: l.append([a,b]) l.sort() for x in l: print(*x)