n = int(input()) d = {} for i in range(n): no = int(input()) m, s = map(int, input().split()) T = list(map(str, input().split())) for t in T: if t in d: d[t] += s else: d[t] = s X = list(d.items()) X.sort(key=lambda x:(-x[1], x[0])) if len(X) > 10: X = X[0:10] for i in range(len(X)): print(*X[i])