import numpy as np n=int(input()) l=[];p=[] for i in range(n): id=int(input()) m,s=map(int, input().split()) for x in input().split(): if x in l: p[l.index(x)]+=s else: l.append(x);p.append(s) l=[l[i] for i in np.argsort(p)[::-1]] p=sorted(p)[::-1] for i in range(len(l)): d=1 for j in range(1,len(l)-i): if p[i]==p[i+j]: d+=1 if d>1: l[i:i+d]=sorted(l[i:i+d]) for i in range(min(10,len(l))): print(l[i],p[i])