n = int(input()) dic = {} for i in range(n): no = int(input()) m, s = map(int, input().split()) tag = input().split() for j in range(m): if tag[j] not in dic: dic[tag[j]] = s else: dic[tag[j]] += s ans = sorted(dic.items(), key=lambda x: (-x[1], x[0])) cnt = 0 for k, v in ans: print(k, v) cnt += 1 if cnt == 10: break