n=int(input()) cnt={} for _ in range(n): input() _,m=map(int,input().split()) for i in input().split(): if i not in cnt: cnt[i]=m else: cnt[i]+=m cnt=sorted(cnt.items(),key=lambda x:(-x[1],x[0])) for i in range(min(10,len(cnt))): print(cnt[i][0],cnt[i][1])