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

#define SIZE 1000

int w, n;
int dp[SIZE + 1][SIZE + 1];
int va[SIZE], wa[SIZE];

int main() {
	int u = 0;
	while (cin >> w) {
		if (w == 0) break;
		cin >> n;
		u++;

		char t;
		for (int i = 0; i < n; i++) {
			cin >> va[i] >> t >> wa[i];
		}

		dp[1001][1001] = { 0 };
		for (int i = 1; i <= n; i++) {
			for (int j = 0; j <= w; j++) {
				if (wa[i] <= j) {
					dp[i][j] = max(dp[i - 1][j], dp[i - 1][j - wa[i]] + va[i]);
				} else {
					dp[i][j] = dp[i - 1][j];
				}
			}
		}

		int mv = 0, mw;
		for (int i = 0; i <= w; i++) {
			if (dp[n][i] > mv) {
				mv = dp[n][i];
				mw = i;
			}
		}
		cout << "Case " << u << ":" << endl;
		cout << mv << endl;
		cout << mw << endl;
	}
	return 0;
}