from collections import defaultdict n = int(input()) tags_dict = defaultdict(int) for i in range(n): no = input() m, s = input().split() tags = input().split() for tag in tags: tags_dict[tag] += int(s) tags_list = sorted(tags_dict.items(), key=lambda x:(-x[1], x[0])) for tag in tags_list[:10]: print(tag[0], tag[1])