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