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
  where
    dp = array (0, nt) [(i, f i) | i <- [0..nt]]
    -- i ???????????§??§????????? wmax ??\?????¨???????????§???????????¨??????
    f 0 = let ti@(vi, wi) = ts !! 0 in if wi <= wmax then ti
                                                     else (0, 0)
    f i = let ti@(vi, wi) = ts !! i
              tp@(vp, wp) = dp ! (i - 1)
          in  if wi > wmax
                 then tp
                 else if (wp + wi) <= wmax
                         then maximum' [ti, tp, (vp + vi, wp + wi)]
                         else maximum' [ti, tp]

    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