#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;

void solve(int c,int n,int limit) {
	int i, j, t;
	int w[1000] = {}, v[1000] = {};
	static int dp[1001][1001];

	memset(dp, 0, sizeof(dp));

	for (i = 0;i < n;i++) {
		scanf("%d,%d", &v[i], &w[i]);
		//cin >> v[i] >> w[i];
	}

	for (i = 1;i <= n;i++) {
		for (j = 0;j <= limit;j++) {
			t = j - w[i-1];
			if (t >= 0&&dp[i-1][j]<dp[i-1][t]+v[i-1]) {
				dp[i][j] = dp[i - 1][t] + v[i - 1];
			}
			else {
				dp[i][j] = dp[i - 1][j];
			}
		}
	}

	for (i = 0;i <= limit;i++) {
		if (dp[n][i] == dp[n][limit]) {
			break;
		}
	}

	printf("Case %d:\n%d\n%d\n",c, dp[n][i], i);

	return;
}

int main(void) {
	int c, n, limit;

	c = 1;
	while (true) {
		cin >> limit;
		if (limit == 0) {
			break;
		}
		cin >> n;
		solve(c, n, limit);
		c++;
	}
	
	return 0;
}
