data=[] for i in range(int(input())): input() a,sc=list(map(int,input().split())) tag=input().split() data.append([sc,tag]) score={} for i in range(len(data)): for j in data[i][1]: if j in score: score[j]+=data[i][0] else: score[j]=data[i][0] lsc=[] for i in score: lsc.append([i,score[i]]) lsc.sort(key=lambda x:(-x[1],x[0])) for i in range(10): try: print(lsc[i][0]+" "+str(lsc[i][1])) except: break