import sys
input = lambda :sys.stdin.readline().rstrip()
ii = lambda: int(input())
mi = lambda: map(int, input().split())
ms = lambda: map(str, input().split())
mf = lambda: map(float, input().split())
li = lambda: list(mi())

from collections import defaultdict
N = ii()
d = defaultdict(int)
for i in range(N):
    S = input()
    d[S] += (i+1) * (N-i)

d = sorted(d.items(), key=lambda x:x[0])
for i in range(len(d)):
    print(d[i][1], d[i][0])