f=lambda:input().split() N,=f() d={} for _ in range(int(N)): f() a,b=f() p=f() for s in p: d[s]=d.get(s,0)+int(b) z=sorted(d.items(), key=lambda x:(-x[1],x[0])) for i in range(min(10,len(z))): print(*z[i])