n = int(input()) c = {} count = 0 for i in range(n): no = int(input()) m, p = map(int, input().split()) t = list(input().split()) for s in t: if s in c: c[s] += p else: c[s] = p c = sorted(c.items()) c = sorted(dict(c).items(), key=lambda x: x[1], reverse=True) for k, v in c: count += 1 print(k, v) if count == 10 or count >= len(c): break