//-AOJ0042 -----------------------------------------------------------//
#include <iostream>
#include <vector>
#include <algorithm>
#include <cstdio>
#include <string>

#define rep(x,to) for(int x=0;x<to;x++)
#define rep2(x,from,to) for(int x=from;x<to;x++)

struct stk{
	int kt,wg,ct; 
	stk(int kt,int wg,int ct) : kt(kt), wg(wg), ct(ct) { }
};
using namespace std;


int main(void){

int w,n,cnt=0;
char k;
	
	while(!cin.eof()){
		cin >> w ;
		if(cin.eof() || w==0) break;
		cin.ignore();
		cin >> n;
		vector<vector<int> >dp(n+1,vector<int>(w+1,0));
		vector<int> tk(n,0);
		vector<int> tw(n,0);

		cnt++;
		rep(i,n) cin >> tk[i] >> k >> tw[i];

		rep(i,n) rep(j,w+1){
			if(tw[i]>j){
				dp[i+1][j]=dp[i][j];
			}else{
				dp[i+1][j]=max(dp[i][j],dp[i][j-tw[i]]+tk[i]);
			}
		}	
//		rep(i,n+1) {rep2(j,1,w+1){
//			cout << dp[i][j] << "\t";
//		} cout <<endl;}

		while(dp[n][w]==dp[n][w-1]){
			w--;
		}
		printf("Case %d:\n%d\n%d\n",cnt,dp[n][w],w);
		dp.clear(); tk.clear(); tw.clear();

	}
	
	return 0;
}