from collections import defaultdict N = int(input()) D = defaultdict(int) for _ in range(N): n = int(input()) m, s = map(int, input().split()) tag = input().split() for t in tag: D[t] += s res = [(k, v) for k, v in D.items()] res.sort(key=lambda x:(-x[1], x[0])) print('\n'.join(map(str, list(' '.join(map(str, r)) for r in res[:10]))))