#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <cstring>
#include <cstdio>
#include <queue>
#include <stack>
#include <map>
#include <set>

#define rep(i, n) for(int i = 0; i < n; i++)
#define FOR(i, a, b) for(int i = a; i < b; i++)
#define all(v) (v).begin(), (v).end()
#define MP make_pair

using namespace std;


int v[1010];
int w[1010];

int dp[1010][1010];

void solve(int n, int weight){
	for(int i = 1; i <= n; i++){
		for(int j = 0; j <= weight; j++){
			if(j < w[i]){
				dp[i][j] = dp[i-1][j];
			}else{
				dp[i][j] = max(dp[i-1][j - w[i]]+v[i], dp[i-1][j]);
			}
		}
	}
	int bestval = dp[n][weight];
	int bestwei = weight;
	for(int i = weight; i >= 0; i--){
		if(dp[n][i] == bestval){
			bestwei = i;
		}else{
			break;
		}
	}

	cout << bestval << endl;
	cout << bestwei << endl;
}

int main(){
	int W;
	int casecnt = 1;
	while(cin >> W, W){
		memset(dp, 0, sizeof(dp));
		int n;
		cin >> n;
		
		rep(i, n){
			scanf("%d,%d", &v[i+1], &w[i+1]);
		}
		cout << "Case " << casecnt++ << ':' << endl;
		solve(n, W);
	}


	return 0;
}