#include <iostream>
#include <cstdio>
#include <vector>
#include <cmath>
using namespace std;

#define MAX_W 1000
#define MAX_N 10000
typedef vector<pair<int, int> > vec;

int main(){
	int W, N, c = 1;
	while( cin >> W, W ){
		cin >> N;
		vector<vec> dp(N+1, vec(W+1));
		vector<int> w(N), v(N);
		for( int i = 0; i < N; i++ ){
			scanf("%d,%d", &v[i], &w[i]);
		}
		dp[0][0] = make_pair(0, 0);
		for( int i = 0; i < N; i++ ){
			for( int j = 0; j <= W; j++ ){
				if( j-w[i] >= 0 ){
					if( dp[i][j].first < dp[i][j-w[i]].first+v[i] ){
						dp[i+1][j].first = dp[i][j-w[i]].first+v[i];
						dp[i+1][j].second = dp[i][j-w[i]].second+w[i];
					}else if( dp[i][j].first == dp[i][j-w[i]].first+v[i] ){
						if( dp[i][j].second < dp[i][j-w[i]].second+w[i] ){
							dp[i+1][j].first = dp[i][j].first;
							dp[i+1][j].second = dp[i][j].second;
						}else{
							dp[i+1][j].first = dp[i][j-w[i]].first+v[i];
							dp[i+1][j].second = dp[i][j-w[i]].second+w[i];
						}
					}else{
						dp[i+1][j].first = dp[i][j].first;
						dp[i+1][j].second = dp[i][j].second;
					}
				}else{
					dp[i+1][j] = make_pair(dp[i][j].first, dp[i][j].second);
				}
			}
		}
		/*for( int i = 0; i <= N; i++ ){
			printf("%d: ", i);
			for( int j = 0; j <= W; j++ ){
				printf("%d ", dp[i][j].second);
			}
			printf("\n");
		}*/
		printf("Case %d:\n%d\n%d\n", c, dp[N][W].first, dp[N][W].second);
		c++;
	}
}