#include<cstdio>
#include<iostream>

const int MAX_ITEMS = 1001;

using namespace std;

class CItem{
public:
	CItem(){
		val = 0;
		wgh = 0;
	}
	int val;
	int wgh;
	bool operator<(const CItem &a) const {
		if( val < a.val ){
			return true;
		}else if( val == a.val ){
			if( wgh > a.wgh ){
				return true;
			}
		}
		return false;
	}
	CItem operator+(const CItem &a){
		CItem ret;
		ret.val = val + a.val;
		ret.wgh = wgh + a.wgh;
		return ret;
	}
};

void compute( CItem *Items, CItem **D, int H, int W ){
	for(int i = 1; i < H; i++){
		for(int j = 1; j < W; j++){
			CItem zeroItem;
			CItem m1 = D[i - 1][j];
			CItem m2 = j - Items[i].wgh < 0 ? zeroItem : D[i - 1][j - Items[i].wgh] + Items[i];
			D[i][j] = max( m1, m2 );
		}
	}
}

int main(void){
	int datasetIndex = 0;
	while( true ){
		CItem **D;
		CItem Items[MAX_ITEMS];
		int W, N;

		datasetIndex++;

		scanf("%d",&W);if( W == 0 )break;
		scanf("%d",&N);N++;W++;

		for(int i = 1; i < N; i++){
			scanf("%d,%d", &Items[i].val, &Items[i].wgh);
		}

		D = new CItem*[N];
		for(int i = 0; i < N; i++){
			D[i] = new CItem[W];
		}

		compute( Items, D, N, W );

		printf("Case %d:\n%d\n%d\n", datasetIndex, D[N-1][W-1].val, D[N-1][W-1].wgh);

		for(int i = 0; i < N; i++){
			delete[] D[i];
		}
		delete[] D;
	}
	return 0;
}