import scala.io.StdIn.{readLine,readInt}

object Main extends App {

  var index = 1
  Iterator.continually(readInt).takeWhile ( w => w != 0).foreach { w =>
    val n = readInt
    val wv = Array.ofDim[Int](n+1,2)
    val m = Array.ofDim[Int](n+1,w+1)
    var max = 0
    var maxw = 0

    for(i<-1 to n) wv(i) = readLine.split(",").map(_.toInt)
    for(i<-1 to n;j<-0 to w if j>= wv(i)(1)) {
          if(m(i-1)(j) < wv(i)(0) + m(i-1)(j-wv(i)(1))) {
            m(i)(j) = wv(i)(0) + m(i-1)(j-wv(i)(1))
            if(max < m(i)(j)) {
              maxw = j
              max = m(i)(j)
            }
          } else {
            m(i)(j) = m(i-1)(j)
          }
    }
    println("Case "+index+":")
    println(m(n)(w))
    println(maxw)
    index += 1
  }
}