#include <algorithm>
#include <map>
#include <vector>
#include <iostream>
#include <sstream>
#include <cstdio>
#include <cstring>
#include <cctype>
#include <cstdlib>
#include <cmath>
#include <string>
#include <queue>
#include <stack>
#include <set>
using namespace std;

#define REP(i,n) for(int i=0; i<n; ++i)
#define IREP(it,array) for(vector<int>::iterator it=array.begin(); it!=array.end(); ++it)
#define SREP(it,array) for(vector<string>::iterator it=array.begin(); it!=array.end(); ++it)

#define MP       make_pair
#define PB       push_back
#define ALL(x)   (x).begin(),(x).end()

const int INF = 1<<29;
const double EPS = 1e-9;
double zero(double d){
  return d < EPS ? 0.0 : d;
}

typedef long long LL;
typedef pair<string,string> P;


vector<P> graph; 
set<string> delay;
set<string> delay2;

int main()
{
  cout.setf(ios::fixed, ios::floatfield);
  cout.precision(7);

  int targetNum=0;
  int W,N;    
  while(cin >> W, W){
    targetNum++;
    cin >> N;

    int v[N], w[N];
    REP(i,N){
      int co,we;
      char b;
      cin >> co >> b >> we;
      v[i] = co;
      w[i] = we;
    }

    int dp[N+1][W+1];
    memset(dp,0,sizeof(dp));

    REP(i,N){
      REP(j,W+1){
	if( j - w[i] < 0 )
	  dp[i+1][j] = dp[i][j];
	else{
	  dp[i+1][j] = max( dp[i][j], dp[i][j-w[i]] + v[i] );
	}
      }
    }

    cout << "Case " << targetNum << ":" << endl;

    int rj=0;
    REP(j,W+1){
      if( dp[N][rj] < dp[N][j] ){
	rj = j;
      }
    }


    cout << dp[N][rj] << endl << rj << endl;
  }

  return 0;
}