#include <iostream>
#include <algorithm>
#include <utility>
#include <string>
#include <sstream>
using namespace std;

const int W_MAX = 1000;
const int N_MAX = 1000;

int v[N_MAX + 1], w[N_MAX + 1];
int N,W;


typedef pair<int,int> P;

P bfs(int i, int j)
{
	if(i == N)return P(0,0);
	if(w[i] > j){
		return bfs(i+1,j);
	}
	else{
		P p1,p2;
		p1 = bfs(i + 1,j);
		p2 = bfs(i + 1,j - w[i]);
		p2.first += v[i];p2.second += w[i];
		if(p1.first == p2.first){
			if(p1.second < p2.second){
				return p1;
			}
			else{
				return p2;
			}
		}
		else{
			return max(p1,p2);
		}
	}
}

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];
		}
		cout << "Case " << c++ << ':' << endl;
		P p = bfs(0,W);
		cout << p.first << endl << p.second << endl;
	}
}