import Control.Monad

main :: IO ()
main = do
  m <- readLn
  if m == 0
    then return ()
    else do
      ls <- replicateM m getLine
      let ls' = map (map read . words) ls
      mapM_ putStrLn $ solve ls'
      main

solve :: [[Int]] -> [String]
solve ls = map format (sort (map score ls))
  where
    score :: [Int] -> (Int,Int)
    score (x:xs) = (x,(sumOf (lane xs)))
      where
        lane :: [Int] -> [[Int]]
        lane [] = []
        lane [x] = [[x]]
        lane (x:y:r)
          | x >= 10 = [x]:(lane (y:r))
          | otherwise = [x,y]:(lane r)
    
        sumOf :: [[Int]] -> Int
        sumOf [[x],[y],[z]] = x + y + z
        sumOf [[x],[y1,y2]] = x + y1 + y2
        sumOf [[x1,x2],[y]] = x1 + x2 + y
        sumOf [xs] = sum xs
        sumOf ([x]:[y]:r) = x + y + (head (head r)) + sumOf ([y]:r)
        sumOf ([x]:[y1,y2]:r) = x + y1 + y2 + (sumOf ([y1,y2]:r))
        sumOf ([x1,x2]:r)
          | x1 + x2 >= 10 = x1 + x2 + (head (head r)) + (sumOf r)
          | otherwise = x1 + x2 + (sumOf r)
    sort :: [(Int,Int)] -> [(Int,Int)]
    sort [] = []
    sort ((a,b):r) = (sort higher) ++ [(a,b)] ++ (sort lower)
      where
        higher = filter (\(x,y) -> (y > b) || ((y == b) && (x <= a))) r
        lower = filter (\(x,y) -> (y < b) || ((y == b) && (x > a))) r
    format :: (Int,Int) -> String
    format (a,b) = (show a) ++ " " ++ (show b)