import Control.Arrow
import Control.Monad
import Data.List
import qualified Data.ByteString.Char8 as B
import qualified Data.IntSet as S
import Data.Ord

main :: IO ()
main = readLn >>= readContents >>= print . solve 

solve :: [[Int]] -> Int
solve xs = calc acc0 . sortBy (flip compare) $ edit xs
    where
        calc acc (x : _ : zs) = calc (acc + x) zs
        calc acc [x] = acc + x
        calc acc _ = acc

        acc0 = negate . sum $ map last xs

        edit = map sum 

readLine :: IO [Int]
readLine = unfoldr (fmap (second (maybe B.empty snd . B.uncons)) . B.readInt) <$> B.getLine

readContents :: Int -> IO [[Int]]
readContents = flip replicateM readLine