import java.util.*
import kotlin.collections.HashMap

fun main(args: Array<String>) {
    val sc = Scanner(System.`in`)
    val n = sc.nextInt()
    val dict = HashMap<String, Long>()
    for (i in 1..n) {
        val key = sc.next()
        dict.put(key, dict.getOrDefault(key, 0) + sc.nextLong())
    }
    for (pair in dict.toList().sortedWith(compareBy({it.first.length}, {it.first}))) {
        println("${pair.first} ${pair.second}")
    }
}
