#include <iostream>
#include <sstream>
#include <string>
#include <algorithm>
#include <vector>
#include <map>
#include <set>
#include <bitset>
#include <queue>
#include <stack>
#include <utility>
#include <cstring>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cctype>
using namespace std;
 
#define rep(i,n) for(int i=0; i<n; i++)
#define repa(i,s,e) for(int i=s; i<=e; i++)
 
typedef pair<int,int> pii;
typedef vector<int> vi;
typedef long long ll;

int W,N;
const int MAX_N = 1001;
const int MAX_W = 1001;
int v[MAX_N];
int w[MAX_N];
int dp[MAX_N][MAX_W];

void solve() {
	memset(dp, 0, sizeof(dp));
	repa(i,1,N) {
		repa(j,0,W) {
			if(j - w[i] >= 0) {
				dp[i][j] = max(dp[i-1][j], dp[i-1][j-w[i]] + v[i]);
			}
		}
	}

	int maxval = -1;
	int weight = 0;
	repa(i,0,W) {
		if(maxval < dp[N][i]) {
			maxval = dp[N][i];
			weight = i;
		}
	}
	cout <<	maxval << endl;
	cout << weight << endl;
}

int main() {
	int c = 0;
	while(cin>>W, W) {
		cin >> N;
		rep(i,N) {
			scanf("%d,%d", &v[i], &w[i]);
		}

		printf("Case %d:\n", ++c);
		solve();
	}
}