from collections import defaultdict n = int(input()) dic = defaultdict(int) for i in range(n): s = input() dic[s] += (i + 1) * (n - i) for p, c in sorted(dic.items()): print(c, p)