import Control.Monad (replicateM)
import Data.List (foldl')
import Data.List.Split (splitOn)

type Weight = Int
type Price = Int

main = main' 1
    where
    main' cn = do
        mw <- read `fmap` getLine
        if mw == 0 then return () else do
            n <- read `fmap` getLine
            wps <- (map ((\[p,w]->(w,p)) . map read . splitOn ","))
                `fmap` replicateM n getLine

            let
               (aw,ap) = solve mw wps

            putStrLn ("Case " ++ (show cn) ++ ":")
            print ap
            print aw
            
            main' (cn+1)


solve :: Weight -> [(Weight,Price)] -> (Weight,Price)
solve mw wps = head $ isMaxPrice $ foldl' (update mw) [(0,0)] wps
    where
    isMaxPrice wps = filter (\(_,p) -> p == mp) wps
        where
        mp = maximum $ map snd wps

update :: Weight -> [(Weight,Price)] -> (Weight,Price) -> [(Weight,Price)]
update mw wps (w',p') = merge wps wps'
    where
    wps' = filter (\(w,_) -> w <= mw) $ map (\(w,p) -> (w+w', p+p')) wps

merge :: [(Weight,Price)] -> [(Weight,Price)] -> [(Weight,Price)]
merge wps1 [] = wps1
merge [] wps2 = wps2
merge ((w1,p1):wps1) ((w2,p2):wps2)
    | w1 == w2 = (w1,(max p1 p2)) : merge wps1 wps2
    | w1 <  w2 = (w1,p1)          : merge wps1 ((w2,p2):wps2)
    | w1 >  w2 = (w2,p2)          : merge ((w1,p1):wps1) wps2