N=int(input()) list1=[] list2=[] for i in range(N): a=input() b=input() b=b.rstrip().split(" ") b1=int(b[0]) b2=int(b[1]) c=input() c=c.rstrip().split(" ") for j in range(b1): if c[j] not in list1: list1.append(c[j]) list2.append([b2,c[j]]) #print(list1) #print(list2) list3=[] for k in range(len(list1)): list3.append([10000,list1[k]]) #print(list3) for l in range(len(list2)): for m in range(len(list3)): if list2[l][1]==list3[m][1]: list3[m][0]-=list2[l][0] list3.sort() #print(list3) if len(list3)<=10: i=0 for i in range(len(list3)): print(list3[i][1]+" "+str(10000-list3[i][0])) else: i=0 for i in range(10): print(list3[i][1]+" "+str(10000-list3[i][0]))