t={} n=int(input()) for i in range(n): no=input() m,s=map(int,input().split()) l=input().split() for j in l: if j not in t: t.update({j:s}) else: t[j]+=s t={k:v for k,v in sorted(t.items(), key=lambda x:x[1],reverse=True)} v=sorted([x for x in set(t.values())],reverse=1) c=0 while c<10: s=v.pop(0) d=((k,v) for k,v in sorted({k:v for k,v in t.items() if v==s}.items())) for i,j in d: print(i,j) c+=1