t = int(input()) d = {} for i in range(t): no = int(input()) tc, sc = [int(x) for x in input().split()] for tn in input().split(): if not tn in d.keys(): d[tn] = 0 d[tn] += sc for i, (k, v) in enumerate(sorted(d.items(), key=lambda x: (-x[1], x[0]), reverse=False)): print(k, v) if i == 9: break