#include <iostream>
using namespace std;
#define REP(i,n) for (int i=0; i<n; i++)

int main(void){
	int c=0, W;
	char s;
	while (true){
		cin>>W;
		if (W==0) break;
		int dt[1001];
		REP(i,1001) dt[i]=0;
		c ++;

		int p,w,N;
		cin>>N;
		REP(i,N){
			cin>>p>>s>>w;
			for (int w0=W; w0>=0; w0--){
				int p0=dt[w0];
				if (w0==0 || p0!=0){
					int w1 = w0+w;
					int p1 = p0+p;
					if (w1<=W && dt[w1]<p1)	dt[w1]=p1;
				}
			}
		}
		int m=0,n;
		REP(i, W+1)
			if (dt[i]>m){
				m = dt[i];
				n = i;
			}
		cout<<"Case "<<c<<":"<<endl;
		cout<<m<<endl;
		cout<<n<<endl;
	}
}