#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;

int main(){

	int cnt, N, W, value, weight, v[1001][1001], w[1001][1001];

	cnt = 0;

	while(1){
		cin >> W;
		if(W == 0) break;
		
		cnt++;
		cin >> N;

		for(int i=0; i<=N; i++){
			v[0][i] = 0;
			w[0][i] = 0;
		}
		
		for(int i=1; i<=N; i++){
			scanf("%d, %d", &value, &weight);
			for(int j=0; j<=W; j++){
				if(weight > j){
					v[i][j] = v[i-1][j];
					w[i][j] = w[i-1][j];
				}
				else {
					if(v[i-1][j] < v[i-1][j-weight] + value || (v[i-1][j] == v[i-1][j-weight] + value && v[i-1][j] > w[i-1][j-weight] + weight)){
						v[i][j] = v[i-1][j-weight] + value;
						w[i][j] = w[i-1][j-weight] + weight;
					}
					else {
						v[i][j] = v[i-1][j];
						w[i][j] = w[i-1][j];
					}
				}
			}
		}

		cout << "Case " << cnt << ":" << endl;
		cout << v[N][W] << endl;
		cout << w[N][W] << endl; 
		
	}

	return 0;

}