let arr = (1...Int(readLine()!)!)?.map{
  let input = readLine()!.characters.split(" ").map{String($0)}
  return ($0, input[0], Int(input[1])!)
}
for element in arr.sort({$0.1==$1.1 ? $0.2>$1.2 : $0.1<$1.1}) {
  print(element.0)
}
