use std::io::Write; use std::collections::*; type Map = BTreeMap; fn main() { let mut it = std::io::stdin().lines().map(|p| p.unwrap()); let n = it.next().unwrap().parse::().unwrap(); let mut map = Map::new(); for (i, s) in it.enumerate() { *map.entry(s).or_insert(0) += (i + 1) * (n - i); } let out = std::io::stdout(); let mut out = std::io::BufWriter::new(out.lock()); for (s, c) in map { writeln!(out, "{} {}", c, s).ok(); } }