#include<iostream>
using namespace std;

#define N 1000
#define W 1000
#define INF 10001

#define max(a,b) (a>b?a:b)

int main() {
	static int dp[N+1][W+1];
	int tw[N+1], tv[N+1];
	int w, n, mv, mw, c=0; char dummy;
	for(int i=0; i<N+1; ++i) {
		for(int j=0; j<N+1; ++j) {
			dp[i][j] = 0;
		}
	}
	while(1) {
		cin >> w;
		if(!w) break;
		c++; mw = INF; mv = 0;
		cin >> n;
		for(int i=1; i<=n; ++i) cin >> tv[i] >> dummy >> tw[i];
		for(int i=1; i<=n; ++i) {
			for(int j=0; j<=w; ++j) {
				if(j-tw[i]>=0) dp[i][j] = max(dp[i-1][j], dp[i-1][j-tw[i]]+tv[i]);
			}
		}
		for(int i=0; i<=w; ++i) {
			if(mv<dp[n][i]) {
				mw = i; mv = dp[n][i];
			}
		}
		cout << "Case " << c << ":" << endl;
		cout << mv << "\n" << mw << endl;
	}
	return 0;
}