N = int(input()) ans = dict() for i in range(N): num = int(input()) M, score = map(int, input().split()) word = list(map(str, input().split())) for j in range(M): if word[j] in ans: ans[word[j]] += score else: ans[word[j]] = score ans = sorted(ans.items(), reverse=False, key=lambda x:x[1]) for i in range(min(len(ans), 10)): print(ans[-i-1][0], ans[-i-1][1])