#include <iostream>
#include <iomanip>
#include <sstream>
#include <stdio.h>
#include <list>
#include <vector>
#include <stack>
#include <queue>
#include <map>
#include <algorithm>
#include <math.h>
#include <utility>
#include <string>											
#include <ctype.h>
#include <cstring>
#include <cstdio>
#include <sstream>
#include <functional>

using namespace std;

#define FOR(i,k,n) for(int i = (k); i < (n); i++)
#define REP(i,n) FOR(i,0,n)
#define INF 114514810
#define ll long long
typedef pair<int, int> P;
int W, N;
int v[1005]; int w[1005];
P dp[1005][1005];

P solve(int i, int j)
{
	if (i == N) return P(0, 0);
	if (dp[i][j].first >= 0) return dp[i][j];
	if (j < w[i]) return (solve(i + 1, j));
	else
	{
		P a = solve(i + 1, j); P b = solve(i + 1, j - w[i]);
		b.first += v[i]; b.second += w[i];
		if (a.first != b.first) return max(a, b);
		else
		{
			if (a.second>b.second) return b;
			else return a;
		}
	}
}

int main()
{
	int cnt = 0;
	char c = ',';
	while (cin >> W, W)
	{
		memset(dp, -1, sizeof(dp));
		cnt++;
		cin >> N;
		REP(i, N) cin >> v[i] >> c >> w[i];
		cout << "Case :" << cnt << endl << solve(0, W).first << endl << solve(0, W).second << endl;
	}
	return 0;
}