dic=dict() for i in range(int(input())): input() score=list(map(int,input().split()))[1] strings=input().split() for string in strings: if string not in dic: dic[string]=score else: dic[string]+=score for key in dic.keys(): dic[key]=-dic[key] strings=sorted(list(map(list,dic.items())),key=lambda x:(x[1],x)) for i in range(min(len(strings),10)): print(strings[i][0],-strings[i][1])