N = int(input()) D = {} for _ in range(N): _ = input() M, S = map(int, input().split()) Tag = input().split() for t in Tag: if not t in D: D[t] = S else: D[t] += S D = sorted(D.items(), key = lambda X : (-X[1], X[0])) for k, v in D[:10]: print(k, v)