n = int(input()) dct = dict() for i in range(n): _ = input() c, p = map(int, input().split()) inp = input().split() for j in inp: dct.setdefault(j, 0) dct[j] += p ls = [j for i,j in dct.items()] ls = list(set(ls)) ls.sort() ls.reverse() seq = [] for i in ls: a = [ [k, j] for k, j in dct.items() if j == i] if len(a) == 1: ans = str(a[0][0])+" "+str(a[0][1]) seq.append(ans) else: a.sort() for i in a: ans = str(i[0])+" "+str(i[1]) seq.append(ans) seq = seq[:10] for i in seq: print(i)