import Control.Monad

main :: IO ()
main = do
  n <- readLn
  if n == 0
    then return ()
    else do
      ls <- replicateM n getLine
      let scoreL = map words ls
          ansL = solve scoreL
      mapM_ putStrLn ansL
      main

solve :: [[String]] -> [String]
solve ls = foldl getName [] (sort (deform ls))
  where
    getName :: [String] -> (String,Int,Int) -> [String]
    getName acc (n,v,l) = acc ++ [n]
    deform :: [[String]] -> [(String,Int,Int)]
    deform = map result
      where
        result :: [String] -> (String,Int,Int)
        result (x:xs) = (x, (fst count), (snd count))
          where
            count :: (Int,Int)
            count = count' xs (0,0)
              where
                count' :: [String] -> (Int,Int) -> (Int,Int)
                count' [] t = t
                count' (s:r) (v,d)
                  | s == "0" = count' r (v+1,d)
                  | s == "2" = count' r (v,d+1)
                  | otherwise = count' r (v,d)
    sort :: [(String,Int,Int)] -> [(String,Int,Int)]
    sort [] = []
    sort ((n,v,d):r) = (sort higher) ++ [(n,v,d)] ++ (sort lower)
      where
        lower = [(a,b,c) | (a,b,c) <- r, (b < v) || (b == v && c <= d)]
        higher = [(a,b,c) | (a,b,c) <- r, (b > v) || (b == v && c > d)]
  