#include <iostream>
using namespace std;
int main(){
	int W,N;int count=0;
	while(1){
	cin >> W ; count++;
	if(W==0) break;
	cin >> N ;
	int temp[1000][2];char ch;
	for(int i=0;i<N;i++) cin >> temp[i][0] >> ch >> temp[i][1] ;
	//全探索
	int msum_t,wsum_t;int temp_msum=0;int temp_wsum=0;
	for(int k=0;k<N;k++){
	int stack[1000]={0};int ptr=0;int msum=0;int wsum=0;
	for(int i=k;i<N;i++){
		if(wsum+temp[i][1]<W) do{msum+=temp[i][0];wsum+=temp[i][1];stack[ptr++]=i;}while(0);
		else 
			do{for(int j=0;j<ptr;j++){
			msum_t=msum-temp[stack[j]][0]+temp[i][0];
			wsum_t=wsum-temp[stack[j]][1]+temp[i][1];
			if(msum_t>msum && wsum_t<W) do{msum=msum_t;wsum=wsum_t;stack[j]=i;}while(0);
			else if(msum_t==msum && wsum_t<wsum) do{wsum=wsum_t;stack[j]=i;}while(0);
			};
			}while(0);
	};
	if(msum>temp_msum) do{temp_msum=msum; temp_wsum=wsum;}while(0);
	else if(msum==temp_msum && wsum<temp_wsum) temp_wsum=wsum;
	};
	cout << "Case " << count << ":\n" << temp_msum << '\n' << temp_wsum << '\n' ;
	};
}