def ch(i,w,n,tr,np)
  if i==n
    0
  elsif w<tr[i][1]
    ch(i+1,w,n,tr,np)[0]
  else
    a = ch(i+1,w,n,tr,np)[0]
    b = ch(i+1,w-tr[i][1],n,tr,np+[[tr[i][1]]])[0]+tr[i][0]
    np << tr[i][1] if a<b
    [[a,b].max,np]
  end
end

i = 0
while gets
  w,n = $_.to_i,gets.to_i
  break if w == 0
  tr,np = [],[]
  n.times do
    tr << gets.split(",").map(&:to_i)
  end
  ans = ch(0,w,n,tr,np)
  puts "Case #{i+=1}:\n#{ans[0]}\n#{ans[1].inject(:+)}"
end