#define rep(i,n) for(int i=0;i<(int)(n);i++)
#define ALL(v) v.begin(),v.end()
typedef long long ll;
 
#include<bits/stdc++.h>
using namespace std;
 
int main(){
  ios::sync_with_stdio(false);
  std::cin.tie(nullptr);
  
  int n;
  cin>>n;
  map<string,int> m;
  for(int i=0;i<=n;i++){
    string s;
    getline(cin,s);
    if(i==0) continue;
    m[s]+=i*(n+1-i);
  }
  for(auto [a,b]:m){
    cout<<b<<" "<<a<<endl;
  }
    
  return 0;
}