#include <bits/stdc++.h>
using namespace std;

#define rep(i, n) for( ll i = 0; i < n; i++ )
using ll = long long;

int main() {
	int N;
	cin >> N;
	cin.ignore(numeric_limits<streamsize>::max(), '\n');
	map<string, ll> mp;
	string s;
	rep(i, N) {
		getline(cin, s);
		ll n = i + 1;
		mp[s] += n * (N - i);
	}
	for(auto it = mp.begin(); it != mp.end(); it++ ) cout << it->second << " " << it->first << endl;
}