#include<iostream>
#include<vector>
#include<climits>
using namespace std;

int main()
{
	int W, N;
	vector<int> value, weight;
	int sumV, sumW;
	int maxV, minW;
	int a, b;
	int i, j;
	int count;

	count = 0;
	while(cin>>W, W) {
		count++;
		cin>>N;
		value.clear(); weight.clear();
		for (i = 0; i < N; i++) {
			scanf("%d,%d", &a, &b);
			value.push_back(a);
			weight.push_back(b);
		}
		maxV = 0; minW = INT_MAX;
		for (i = 0; i < (1<<N); i++) {
			sumV = 0; sumW = 0;
			for (j = 0; j < N; j++) {
				if ( !(i>>j & 1) ) continue;
				sumV += value[j];
				sumW += weight[j];
			}
			if (sumV == maxV) {
				if (sumW < minW) minW = sumW;
			} else if (sumV > maxV && sumW <= W) {
				maxV = sumV;
				minW = sumW;
			}
		}
		cout<<"Case "<<count<<":"<<endl;
		cout<<maxV<<endl;
		cout<<minW<<endl;
	}

	return 0;
}