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 strings=sorted(list(map(list,dic.items())),key=lambda x:(x[1],x),reverse=True) for i in range(min(len(strings),10)): print(strings[i][0],strings[i][1])