#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];
				}
			}
		}

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