N = int(input()) dic = {} for i in range(N): no = int(input()) M, S = map(int, input().split()) tags = list(input().split()) for t in tags: dic[t] = dic.get(t, 0) + S l = [[k, v] for k, v in dic.items()] l.sort(key = lambda x: (-x[1], x[0])) for i in range(min(len(l), 10)): print(l[i][0], l[i][1])