from collections import defaultdict n = int(input()) dt = {} A = [] for i in range(n): _ = input() _, s = map(int, input().split()) T = input().split() for t in T: if not t in dt: dt[t] = len(dt) A.append([0, t]) A[dt[t]][0] += s A.sort(key=lambda x: (-x[0], x[1])) for a, b in A[:10]: print(b, a)