function main() {
  var
  c = 1,
  i, j, res,
  w, n, ws, vs;
  for(i = 0; w = parseInt(input[i++], 10); c++) {
    n = parseInt(input[i++], 10);
    ws = [];
    vs = [];
    for(j = 0; j < n; j++) {
      line = input[i++].split(',').map(parseFloat);
      vs.push(line[0]);
      ws.push(line[1]);
    }
    console.log('Case %d:', c);
    console.log(thief(n, ws, vs, w).join('\n'));
  }
}

function max(a, b) {
  return a > b ? a : b;
}

function thief(n, ws, vs, m) {
  var
  i, j, v, w,
  _back,
  back = {};
  for(i = 0; i < n; i++) {
    w = ws[i];
    v = vs[i];
    _back = [];
    for(j = 0; j <= m; j++) {
      if(j < w) {
        _back[j] = back[j]||0;
      } else {
        _back[j] = max(back[j]||0, (back[j - w]||0) + v);
      }
    }
    back = _back;
  }
  w = v = 0;
  for(j = 0; j <= m; j++) {
    if(v < back[j]) {
      w = j;
      v = back[j];
    }
  }
  return [v, w];
}

var
input = '';

process.stdin.resume();
process.stdin.setEncoding('utf8');
process.stdin.on('data', function(chunk) {
  input += chunk;
});
process.stdin.on('end', function() {
  input = input.split('\n');
  main();
});