tag = {} n = int(input()) for i in range(n): input() m, s = [ int(v) for v in input().split() ] tags = input().split() for j in tags: if j not in tag: tag[j] = s else: tag[j] += s ans_list = [(-tag[i], i) for i in tag] ans_list.sort() for v, i in ans_list[:10]: print(i, -v)