import scala.annotation.tailrec

object Main extends App {

  import scala.io.StdIn._

  solve
  @tailrec
  def solve: Unit = {
    val n = readLine.trim.toInt
    if (n == 0) return
    val teams = Array.tabulate(n){_ ⇒
      val arg = readLine.trim.split(' ')
      (arg.head, Result(arg.tail.map(_.toInt)))
    }.sortBy(_._2)
    println(teams.map{_._1}.mkString("\n"))
    solve
  }
  object Result{
    def apply(result: Array[Int]): Result = {
      Result(result.count(_ == 0), result.count(_ == 2), result.count(_ == 1))
    }
  }
  case class Result(win: Int, draw: Int, lose: Int) extends Ordered[Result] {
    override def compare(that: Result): Int = if (win == that.win) lose.compare(that.lose) else that.win.compare(win)
  }
  implicit class Extension[T](value :T){
    @inline
    def let[A](@inline func: T ⇒ A): A = func(value)
    @inline
    def also(@inline func: T ⇒ Unit): T = {func(value); value}
  }
}
