#include <iostream>

using namespace std;

int n, W;
int v[1010], w[1010];
int dp[1010][1010];

void solve() {
	for (int i=0; i<n; i++) {
		for (int j=0; j<=W; j++) {
			if (w[i] <= j) dp[i+1][j] = max(dp[i][j], dp[i][j-w[i]] + v[i]);
			else dp[i+1][j] = dp[i][j];
		}
	}
	
	int sumV = 0, sumW = 0;
	for (int i=0; i<=W; i++) {
		if (dp[n][i] > sumV) {
			sumV = dp[n][i];
			sumW = i;
		}
	}
	cout << sumV << endl;
	cout << sumW << endl;
}

int main() {
	int Case = 0;
	
	while (1) {
		cin >> W; if (W==0) break;
		
		Case++;
		cout << "Case " << Case << ":" << endl;
		
		cin >> n;
		
		char dumy;
		for (int i=0; i<n; i++) cin >> v[i] >> dumy >> w[i];
		
		solve();
	}
	
	return 0;
}