#include <bits/stdc++.h>
using namespace std;

int main() {
	int w_lim;
	for (int t = 1; cin >> w_lim && w_lim; t++) {
		int n;
		cin >> n;
		int v[n], w[n];
		char c;
		for (int i = 0; i < n; i++) {
			cin >> v[i] >> c >> w[i];
		}
		int dp[n + 1][w_lim + 1]{};	//dp[i][j] := 先頭i個見て重さがjの時の価値の最大値
		for (int i = 0; i < n; i++) {
			for (int j = 0; j <= w_lim; j++) {
				dp[i + 1][j] = dp[i][j];
				if (0 <= j - w[i]) {
					dp[i + 1][j] = max(dp[i + 1][j], dp[i][j - w[i]] + v[i]);
				}
			}
		}
		cout << "Case " << t << ':' << endl;
		pair<int, int> ans(0, 0);
		for (int i = 0; i <= w_lim; i++) {
			if (ans.first < dp[n][i]) {
				ans = {dp[n][i], i};
			}
		}
		cout << ans.first << endl;
		cout << ans.second << endl;
	}
	return 0;
}
