n=int(input()) d={} for i in range(n): input() a,b=map(int,input().split()) c=input().split() for j in range(a): e=d.get(c[j]) if e: d[c[j]]=b+e else: d[c[j]]=b for i ,j in sorted(d.items(),key=lambda x:(-x[1])): print(i,j)