$dpt = Hash.new {|h,k| h[k] = Hash.new}

def rec(i,j)
	weight = 0
	if $dpt[i][j]
		return $dpt[i][j]
	end
	if i >= $n
		value = 0 
	elsif j < $w[i]
		value = rec(i+1,j)[:value]
		weight = rec(i+1,j)[:weight]
	else
		value = [$v[i] + rec(i+1,j-$w[i])[:value], rec(i+1,j)[:value]].max
		if $v[i] + rec(i+1,j-$w[i])[:value] > rec(i+1, j)[:value]
			weight = $w[i] + rec(i+1,j)[:weight]
		else
			weight = rec(i+1,j)[:weight]
		end
	end
	$dpt[i][j] = {:value => value, :weight => weight}
end

maxw = nil
c = 1
$w = []
$v = []
maxw = gets.to_i

until maxw==0
	$n = gets.to_i
	$n.times do |i|
		$v[i], $w[i] = gets.split(",").map(&:to_i)
	end
	res = rec(0, maxw)
	print "Case #{c}:\n"
	p res[:value]
	p res[:weight]
	$w = []
	$v = []
	maxw = gets.to_i
	c += 1
end