import collections import heapq def item2score(item): key, value = item return (-value, key) def main(): n = int(input()) tag2score = collections.defaultdict(int) for _ in range(n): _ = input() _, s = (int(x) for x in input().split()) for tag in input().split(): tag2score[tag] += s top_tags = heapq.nsmallest(10, tag2score.items(), key=item2score) for item in top_tags: print(*item) if __name__ == '__main__': main()