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() for i in ls: a = [ [k, j] for k, j in dct.items() if j == i] if len(a) == 1: print(a[0][0], a[0][1]) else: a.sort() for i in a: print(i[0],i[1])