#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

int main(){
	int w;
	const int maxw = 1000;
	int count = 1;
	while(cin >> w){
		if(w == 0) break;
		int n;
		int cost[maxw + 1];
		int best[maxw + 1];
		for(int i = 0; i <= maxw; i++){
			cost[i] = 0;
			best[i] = 0;
		}
		int values[maxw + 1];
		int weights[maxw + 1];
		cin >> n;
		for(int i = 0; i < n; i++){
			char c;
			cin >> values[i] >> c >> weights[i];
		}
		for(int j = 0; j < n; j++){
			for(int k = 1; k <= w; k++)
				if(k >= weights[j])
					if(cost[k] < cost[k - weights[j]] + values[j])
					{
						cost[k] = cost[k - weights[j]] + values[j];
						best[k] = j;
					}
		}
		cout << "Case" << " " << count++ << ":" << endl;
		for(;w>0; w--) if(cost[w] != cost[w-1]) break;
		cout << cost[w] << endl;
		int total = 0;
		for(int l = w; l - weights[best[l]] >= 0;){
			if(l - weights[best[l]] >= 0){
				total += weights[best[l]];
				l -= weights[best[l]];
			}
		}
		cout << total << endl;
	}
	return 0;
}