from operator import itemgetter N=int(input()) D=dict() for i in range(N): x=int(input()) x,score=map(int,input().split()) L=input().split() for l in L: if l in D: D[l]+=score else: D[l]=score ANS=[] for d in D: ANS.append((d,D[d])) ANS.sort(key=itemgetter(0)) ANS.sort(key=itemgetter(1),reverse=True) for a,x in ANS[:10]: print(a,x)