import collections N = int(input()) d = collections.defaultdict(lambda:0) for i in range(N): input() a,b = map(int,input().split()) tag = input().split() for i in range(a): d[tag[i]] += b tp = [(val,key) for key,val in d.items()] tp.sort(key=lambda x:x[1]) tp.sort(key=lambda x:-x[0]) for v,k in tp[:10]: print(k,v)