n=int(input()) hm={} for _ in range(n): input() m,s=map(int,input().split()) tags=input().split() for tag in tags: if tag not in hm: hm[tag]=0 hm[tag]+=s l=[] for k in hm: l.append((hm[k],k)) l.sort() l.reverse() for i in range(min(10,len(l))): print(l[i][1]+' '+str(l[i][0]))