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

main :: IO ()
main = loop 0
  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 w ts = dp ! (nt, w)
  where
    dp = array ((0, 0), (nt, w)) [((i, j), f i j) | i <- [0..nt], j <- [0..w]]
    -- i ???????????§??§????????? j ??\?????¨????????¨??????????????¨??????
    f 0 _ = (0, 0)
    f _ 0 = (0, 0)
    f i j = let ti@(vi, wi) = ts !! (i - 1)
                tm@(vm, wm) = dp ! (i - 1, j)
            in  if j - wm > wi then maximum' [ti, tm, (vi + vm, wi + wm)]
                               else maximum' [ti, tm]

    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