from collections import defaultdict N = int(input()) st = set() d = defaultdict(int) for i in range(N): s = str(input()) d[s] += (N - i) * (i + 1) st.add(s) lst = sorted(list(st)) for l in lst: print(d[l], l)