#include <iostream>
#include <utility>
#include <vector>
#include <cstring>
using namespace std;
const int MAX_N = 1000,MAX_M = 10000;
int n;
typedef pair<int,int> P;
P ilist[MAX_N];
P memo[MAX_N + 1][MAX_M + 1];

P comp(const P &a,const P &b){
	if(a.first < b.first) return b;
	return a;
}

P dp(int now,int lim){
	if(memo[now][lim].first > -1){
		return memo[now][lim];
	}
	P res;
	if(now == n) return make_pair(0,0);
	else if(lim < ilist[now].second){
		res = dp(now + 1,lim);
	}
	else{
		res = dp(now + 1,lim - ilist[now].second);
		res.second += ilist[now].second;
		res.first += ilist[now].first;
		res = comp(dp(now + 1,lim),res);
	}
	return memo[now][lim] = res;
}

int main(){
	int lim,cnt = 0,w,v;
	P item;
	char buf;
	while(cin >> lim,lim){
		for(int i = 0;i <= MAX_N;++i){
			for(int j = 0;j <= MAX_M;++j){
				memo[i][j] = make_pair(-1,-1);
			}
		}
		++cnt;
		cin >> n;
		for(int i = 0;i < n;++i){
			cin >> v >> buf >> w;
			ilist[i] = make_pair(v,w);
		}
		item = dp(0,lim);
		cout << "Case " << cnt << ":" << endl;
		cout << item.first << endl << item.second << endl;
	}
	return 0;
}