#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <cstring>
#include <map>
#include <queue>
#include <stack>
#include <set>
#include <functional>
#include <sstream>
#include <complex>
#include <climits>
using namespace std;

#define REP(i,a,n) for(int i=(a);i<(int)(n);i++)
#define rep(i,n) REP(i,0,n)
#define pb push_back
#define mp make_pair
#define all(x) x.begin(),x.end()
#define EPS 1e-8

int dp[2][1010];

int main(){
	int w,n;
	pair<int,int> kati[1000];
	for(int T=1; scanf("%d",&w),w; T++){
		printf("Case %d:\n",T);
		memset(dp,0,sizeof(dp));
		scanf("%d",&n);
		rep(i,n){
			int a,b; scanf("%d,%d",&a,&b);
			kati[i].first = a;
			kati[i].second = b;
		}
		rep(i,n){
			int cur = i%2;
			int next = (i+1)%2;
			for(int j=0; j<=w; j++){
				if( kati[i].second>j ){
					dp[next][j] = dp[cur][j];
				}else{
					dp[next][j] = max(dp[next][j],
												max(dp[cur][j-kati[i].second]+kati[i].first,dp[cur][j]));
				}
			}
			rep(i,w+1)dp[cur][i]=0;
		}
		int sum=0,omosa=0;
		rep(i,w+1)if( sum<dp[n%2][i] ){
			sum = dp[n%2][i];
			omosa = i;
		}
		printf("%d\n%d\n",sum,omosa);
	}
	return 0;
}