n = int(input()) scl = [] wdl = [] for i in range(n): scl.append([int(input()), list(map(int, input().split()))]) wdl.append([i, list(map(str, input().split()))]) c = {} for i in range(n): for j in wdl[i][1]: if not j in c: c[j] = scl[i][1][1] else: c[j] += scl[i][1][1] c2 = sorted(c.items(), key=lambda x: (-x[1], x[0])) for wd, sc in c2: print(wd, sc)