n, m = map(int, input().split()) d = {} for _ in range(n + m): s, a = input().split() d[s] = a for p in sorted(d.items()): print(*p)