#include <iostream>
#include <algorithm>
using namespace std;
const int INF = 1 << 19;
const int W_MAX = 1000;
const int N_MAX = 1000;

int v[N_MAX + 1], w[N_MAX + 1];
int N,W;
int dp[N_MAX + 1][W_MAX +1];//N個目までで、Wを超えないように選んだ時の最大値

int main()
{
	int c = 1;
	while(cin >> W,W){
		cin >> N;
		for(int i = 0; i < N; ++i){
			cin >> v[i]; cin.ignore(1,','); cin >> w[i];
		}
		
		for(int i = 0; i < N; ++i){
			for(int j = 0; j <= W; ++j){
				if(j < w[i]){
					dp[i + 1][j] = dp[i][j];
				}else {
					dp[i + 1][j] = max(dp[i][j], dp[i][j-w[i]] + v[i]);
				}
			}
		}
		cout << "Case " << c++ << ':' << endl;
		int max_v = -1, min_w = INF;
		for(int i = 0; i <= W; ++i){
			if(dp[N][i] > max_v){
				max_v = dp[N][i];
				min_w = i;
			}
		}
		cout << max_v << endl << min_w << endl;
	}
}