#include<bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i=0;i<n;i++ )
typedef pair<int,int> mypair;
int main(){
	int w,n;
	int casen = 1;
	while(1){
		cin>>w;
		if(w==0)break;
		cin>>n;
	vector<int> v(n),we(n);
	rep(i,n)scanf("%d,%d",&v[i],&we[i]);
	vector<vector<int> > dp(n+1,vector<int> ( w+1,0) );
	rep(i,n)rep(j,w+1){
	if(j<we[i])dp[i+1][j]=dp[i][j];
	else dp[i+1][j] = max(dp[i][j],dp[i][j-we[i]]+v[i]);
	}
	mypair ans = mypair(0,0);
	rep(i,w+1)ans = max(ans,mypair(dp[n][i],-i));
	cout<<"Case "<<casen<<":"<<endl;
	cout<<ans.first<<endl<<-ans.second<<endl;
		casen++;
	}


	return 0;
}

