import           Control.Monad
import           Data.Array
import           Data.List

main :: IO ()
main = loop 1
  where
    loop c  = do
      w <- readLn
      if w == 0
         then return ()
         else do
           n <- readLn
           xs <- replicateM n getLine
           let xs'      = map (tup . words . map c2s) xs
               (vm, wm) = solve w xs'
           putStrLn $ "Case " ++ show c ++ ":"
           print vm
           print wm
           loop (c + 1)

    c2s ',' = ' '
    c2s c   = c

    tup (x:y:_) = (read x, read y)


solve :: Int -> [(Int, Int)] -> (Int, Int)
solve wmax ts = dp ! (nt, wmax)
  where
    dp = array ((0, 0), (nt, wmax)) [((i, j), f i j) | i <- [0..nt], j <- [0..wmax]]
    -- i ???????????§??§????????? j ??\?????¨????????¨??????????????¨??????
    f 0 w = let ti@(vi, wi) = ts !! 0
            in  if wi <= w then ti else (0, 0)
    f i w = let ti@(vi, wi) = ts !! i
                tp          = dp ! (i - 1, w)
                tps         = [dp ! (i - 1, j) | j <- [0..(w - wi)]]
            in  maximum' $ [ti, tp] ++ [(vpsi + vi, wpsi + wi) | (vpsi, wpsi) <- tps, (wpsi + wi) <= wmax]

    max' t1@(v1, w1) t2@(v2, w2)
      | v1 == v2  = (v1, min w1 w2)
      | otherwise = max t1 t2

    maximum' = foldl max' (0, 0)

    nt = length ts - 1