N = int(input()) tag_d = {} cnt = 0 def printlist(lst,p): global cnt lst.sort() for i in lst: print(i,p) cnt+=1 if cnt==10: exit(0) for i in range(N): input() a,point = map(int,input().split()) tag = list(input().split()) for t in tag: tag_d.setdefault(t,0) tag_d[t]+=point tag_d = sorted(tag_d.items(),key=lambda x:x[1]) tag_l = list(reversed(tag_d)) p = tag_l[0][1] ptr_l = [] for i in tag_l: if i[1]==p: ptr_l.append(i[0]) else: printlist(ptr_l,p) p = i[1] ptr_l = [i[0]] printlist(ptr_l,p)