n = int(input()) d = {} for _ in range(n): input() m, s = map(int, input().split()) l = input().split() for i in l: if not i in d: d[i] = s else: d[i] += s D = sorted(d.items(), key=lambda x: (-x[1], x[0])) if len(D) <= 10: for i in range(len(D)): print(*D[i]) else: for i in range(10): print(*D[i])