import Control.Monad

main :: IO ()
main = do
  loop 1
loop :: Int -> IO ()
loop c = do
  w <- readLn
  if w == 0
  then return ()
  else do
    n <- readLn
    ls <- replicateM n getLine
    let ls' = map (map read . words') ls
        initArray = zip (repeat 0) [0..w]
        ans = solve initArray ls'
    putStrLn $ "Case " ++ (show c) ++ ":"
    print $ fst ans
    print $ snd ans
    loop (c+1)

words' :: String -> [String]
words' s = words'' s [[]]
words'' :: String -> [String] -> [String]
words'' [] new = new
words'' (',':xs) new = words'' xs (new ++ [[]])
words'' (x:xs) new = words'' xs ((init new) ++ [(last new) ++ [x]])

solve :: [(Int,Int)] -> [[Int]] -> (Int,Int)
solve array [] = getMax array
  where
    getMax :: [(Int,Int)] -> (Int,Int)
    getMax array = getMax' (reverse array)
      where
        getMax' :: [(Int,Int)] -> (Int,Int)
        getMax' [(v,w)] = (v,w)
        getMax' ((v1,w1):(v2,w2):r)
          | v1 > v2 = (v1,w1)
          | otherwise = getMax' ((v2,w2):r)
solve array (x:xs) = solve (update array x array) xs
  where
    update :: [(Int,Int)] -> [Int] -> [(Int,Int)] -> [(Int,Int)]
    update [] _ _ = []
    update ((a,b):r) [v,w] array = (a',b):(update r [v,w] array)
      where
        a' = if w > b
          then a
          else max a ((getV (b-w) array) + v)
               where
                 getV :: Int -> [(Int,Int)] -> Int
                 getV w' ((v,w):r)
                   | w' == w = v
                   | otherwise = getV w' r