#include <iostream>
#include <queue>
#include <stack>
#include <vector>
#include <stdio.h>
#include <algorithm>
#include <string.h>
#include <string>
#include <cmath>
#include <complex>
#include <map>
using namespace std;

#define rep(i,n) for(int i=0;i<int(n);++i)

static const double PI=6*asin(0.5);
typedef complex<double> P;
typedef long long ll;


int m;
int w,n;
pair <int,int> dp[1050][1050];
char t;
vector<pair <int,int> > pa;
pair<int,int> tpa;

pair<int,int> maxpair(pair <int,int> a,pair <int,int> b){
	return a.second==b.second ? a.first>b.first ? b:a  :  a.second>b.second ? a:b;
}

pair <int,int> rec(int i,int j){
	if(dp[i][j].second>=0) return dp[i][j];
	if(i==pa.size()){
		tpa.second = 0;
		tpa.first=0;
	}
	else if(j<pa[i].first){
		tpa = rec(i+1,j);
		
	}
	else{
		pair <int,int> tpa1=rec(i+1,j);
		pair <int,int> tpa2=rec(i+1,j-pa[i].first);
		tpa2.first+=pa[i].first;
		tpa2.second+=pa[i].second;
		tpa = maxpair(tpa1,tpa2);
	}
	return dp[i][j] = tpa;
	
}

int main(){
	int e=1;
	while(cin>>w,w!=0){
		memset(dp,-1,sizeof(dp));
		cout<<"Case "<<e++<<":\n";
		tpa.first=0;
		tpa.second=0;
		cin>>n;
		rep(i,n){
			int t1,t2;
			cin>>t1>>t>>t2;
			pair <int,int> tm;
			tm.first=t2;
			tm.second=t1;
			pa.push_back(tm);
		}
		//rep(i,pa.size())cout<<pa[i].first<<" "<<pa[i].second<<endl;
		rec(0,w);
		
		cout<<dp[0][w].second<<endl<<dp[0][w].first<<endl;
		pa.clear();
	}
	return 0;
}