from collections import Counter C = Counter() N = int(input()) for i in range(N) : No = int(input()) M,S = map(int,input().split()) T = input().split() for t in T : C[t] += S A = C.most_common(10) A = sorted(A, key=lambda x: x[0], reverse=False) A = sorted(A, key=lambda x: x[1], reverse=True) for a in A : print(*a)