#coding:utf-8

count = 1

def max(a,b)
  (a>b)? a : b
end



while (w=gets.to_i) != 0 do
	n = gets.to_i #宝の数
	tresure = Array.new(n)


  dp = Array.new(1001){Array.new(1001,-1)}
  for i in 0..n-1 do
    #n個の宝の価値と重さの配列をtresure配列に入れる
    str = gets.chomp.split(',').map(&:to_i)
    
    for j in 0..w do
      dp[j][i+1] = max(dp[j][i], (j-str[1]>=0? dp[j-str[1]][i]+str[0] : 0))
    end
  end

    #表示
    print("Case #{count}:\n")
    for j in 0..w+1 do
      if dp[j][n] == dp[w][n] then
        p dp[j][n]
        p j
        break
      end
    end

  
 
	
  count += 1
  
  
	
end