import Control.Applicative ((<$>))
import Control.Monad (replicateM, unless)
import Data.List (permutations, minimumBy)
import Data.Function (on)

main :: IO ()
main = do
  n <- readLn
  unless (n == 0) $ do
    solve n
    main

solve :: Int -> IO ()
solve n = do
  xs <- replicateM n $ food <$> getLine
  let fs = minimumBy (compare `on` cog) . filter f . permutations $ xs
  mapM_ print fs
  where f :: [Food] -> Bool
        f fs = maybe False (const True) . foldr g (Just 0) $ fs
          where g _ Nothing = Nothing
                g fd (Just w) | w <= (sp fd) = Just (w + (wt fd))
                              | otherwise = Nothing
cog :: [Food] -> Double
cog fs = sum (zipWith (\a b -> (fromIntegral a) * (fromIntegral b)) (map wt fs) [1 ..]) / fromIntegral (sum (map wt fs))

data Food = Food {nm :: String, wt :: Int, sp :: Int}

instance Show Food where
  show (Food n _ _) = n
  
food :: String -> Food
food s = let (n:w:p:_) = words s
         in Food n (read w) (read p)

