import Control.Monad
import Data.List
main = do
  n <- readLn
  sp <- fmap (fmap (reverse.sortOn (snd.snd)).groupOn (fst.snd).sortOn (fst.snd).zip [1..n]) $ replicateM n $ do 
    [s,p] <- words <$> getLine
    return (s,read p :: Int)
  mapM_ (print.fst) $ concat sp


-- | A version of 'group' where the equality is done on some extracted value.
groupOn :: Eq b => (a -> b) -> [a] -> [[a]]
groupOn f = groupBy ((==) `on2` f)
    -- redefine on so we avoid duplicate computation for most values.
    where (.*.) `on2` f = \x -> let fx = f x in \y -> fx .*. f y