#include<iostream>
#include<algorithm>
#include<map>
using namespace std;
typedef pair< int , int > P;

int W, N, value[1000], weight[1000];
P dp[1001][1001];

P rec(int i,int j){ //i番目以降の品物からj以下となるように選ぶ
  if(dp[i][j] != P(-1,-1)) return dp[i][j];
  P res;
  if(i == N) res = P(0,j);
  else if(j < weight[i]) return res = rec(i+1,j);
  else{
    P tmp = rec(i+1,j-weight[i]);
    res = max(rec(i+1,j),P(tmp.first + value[i],tmp.second));
  }
  return dp[i][j] = res;
}

int main(){
  int cnt = 0; char op;
  while(cin >> W , W){
    fill_n(*dp,1001*1001,P(-1,-1));
    cin >> N;
    for(int i = 0 ; i < N ; i++ ) cin >> value[i] >> op >> weight[i];
    P p = rec(0,W);
    cout << "Case " << ++cnt << ":" << endl;
    cout << p.first << endl;
    cout << W - p.second << endl;
  }
}