from collections import defaultdict

def main():
    N = int(input())
    D = defaultdict(int)
    for _ in range(N):
        _ = input()
        _, i = map(int, input().split())
        for s in input().split():
            D[s] += i

    tag = sorted(D.items())
    tag = sorted(tag, key=lambda x: x[1], reverse=True)
    i = 0
    while i < min(len(tag), 10):
        print(*tag[i], sep=' ')
        i += 1

main()