#!/usr/bin/env python3 from collections import defaultdict import sys def main(): N, M = map(int, input().split()) d = defaultdict(int) for _ in range(N): S, A = input().split() A = int(A) d[S] = A for _ in range(M): T, B = input().split() B = int(B) d[T] = B l = sorted(d.items(), key=lambda x:x[0]) for k, v in l: print(k, v) return if __name__ == '__main__': main() # 301, 1 # 300 4 * 300 = 1200