#include <iostream>
#include <cstdio>
#include <cstring>
#include <sstream>
#include <algorithm>
#include <math.h>
#include <map>
#include <functional>
#include <iomanip>
#include <vector>
#include <queue>
#include <stack>
#include <set>
#include <typeinfo>
#define PI 3.14159265359
#define INF 99999999
#define rep(i, n) for(int i=0; i<n; i++)
#define REP(n) rep(i, n)
#define pb push_back
#define mp make_pair
typedef long long ll;
using namespace std;
typedef pair<int, int> P;

//dp[今見ているお宝][それまでに入れたお宝の重さ]
int dp[1024][1024];
int W, N;
int v[1024], w[1024];
char comma;
int maxv, maxw;

int rec(int now, int weight)
{
	if (dp[now][weight] >= 0)
	{
		//printf("dp[%d][%d] = %d\n", now, weight, dp[now][weight]);
		return dp[now][weight];
	}

	int res;
	if (now == N)
	{
		res = 0;
		if (res > maxv)
		{
			maxv = res;
			maxw = weight;
		} else if (res == maxv && maxw > weight) {
			maxw = weight;
		}
	} else if (W - weight < w[now]) {
		res = rec(now + 1, weight);
		if (res > maxv)
		{
			maxv = res;
			maxw = weight;
		} else if (res == maxv && maxw > weight) {
			maxw = weight;
		}
	} else {
		res = max(rec(now + 1, weight), rec(now + 1, weight + w[now]) + v[now]);
		if (res > maxv)
		{
			maxv = res;
			maxw = weight;
		} else if (res == maxv && maxw > weight) {
			maxw = weight;
		}
	}

	//printf("dp[%d][%d] = %d\n", now, weight, res);
	return dp[now][weight] = res;
}

int main()
{
	int cnt = 1;
	while (cin >> W, W)
	{
		cin >> N;
		rep(i, N) cin >> v[i] >> comma >> w[i];

		rep(i, 1024) rep(j, 1024) dp[i][j] = -1;
		maxv = -1;
		maxw = 999999999;

		rec(0, 0);

		printf("Case %d:\n", cnt);
		cout << maxv << endl;
		cout << maxw << endl;

		cnt++;
	}

	return 0;
}