N, M = map(int, input().split()) A = [-1] * N B = [-1] * N C = [-1] * M D = [-1] * M for i in range(N): s, t = input().split() A[i] = s B[i] = int(t) for i in range(M): s, t = input().split() C[i] = s D[i] = int(t) name = sorted(set(A) | set(C)) name_to_i = {n: i for i, n in enumerate(name)} score = [0] * (len(name)) for a, b in zip(A, B): i = name_to_i[a] score[i] = b for c, d in zip(C, D): i = name_to_i[c] score[i] = d for i in range(len(name)): print(name[i], score[i])