from collections import defaultdict n = int(input()) scores = defaultdict(int) for _ in range(n): # Read and ignore No input() m, s = map(int, input().split()) tags = input().split() for tag in tags: scores[tag] += s # Sort the tags by descending score and ascending lex order sorted_tags = sorted(scores.items(), key=lambda x: (-x[1], x[0])) # Output up to 10 tags for item in sorted_tags[:10]: print(f"{item[0]} {item[1]}")