N = int(input()) d = {} for i in range(N): no = int(input()) M, S = map(int, input().split()) l = list(map(str, input().split())) for li in l: try: d[li] += S except: d[li] = S value_list = d.values() value_list = sorted(value_list) value_list.reverse() ck = 0 for li in value_list: if li != ck: keys = [k for k, v in d.items() if v == li] keys.sort() for key in keys: print(key,li) ck = li