use std::{collections::HashMap, io};

fn main() {
    let mut map = HashMap::new();

    let n = read_line().parse::<i32>().unwrap();
    for i in 0..n {
        let s = read_line();
        *map.entry(s).or_insert(0) += (i + 1) * (n - i);
    }

    let mut entries = map.iter().collect::<Vec<_>>();
    entries.sort_by_key(|t| t.0);
    for (k, v) in entries {
        println!("{} {}", v, k)
    }
}

fn read_line() -> String {
    let mut line = String::new();
    io::stdin().read_line(&mut line).unwrap();
    line.trim().to_string()
}