from collections import defaultdict N = int(input()) scores = defaultdict(lambda: 0) for i in range(N): input() M, S = map(int, input().split()) tags = input().split() for t in tags: scores[t] += S ranking = sorted(sorted(scores.items()), reverse=True, key=lambda x: x[1]) for item in ranking[:10]: print(*item)