#include <bits/stdc++.h>
using namespace std;

#define int				long long
#define FOR( i, m, n )  for( int (i) = (m); (i) < (n); (i)++ )
#define REP( i, n )     FOR( i, 0, n )
#define REPR( i, m )	for( int (i) = (m); (i) >= 0; (i)-- )
#define REPONE( i, n )	FOR( i, 1, n + 1 )
#define ALL( a )        (a).begin(), (a).end()
#define MP				make_pair
#define EB				emplace_back

typedef pair<int, int> P;

template<class T>bool chmax( T& a, const T& b ) { if( a < b ) { a = b; return 1; } return 0; }
template<class T>bool chmin( T& a, const T& b ) { if( a > b ) { a = b; return 1; } return 0; }

const int INF = 1e9;
const int MOD = 1e9 + 7;

int dp[1001][10001];

signed main() {
	int W, N, cnt = 1;
	while( cin >> W, W ) {
		cin >> N;
		fill( dp[0], dp[1001], 0 );

		int w = 0, vmax = 0;
		vector<P> tre( N );
		REP( i, N ) {
			char c;
			cin >> tre[i].first >> c >> tre[i].second;
		}

		REP( i, N ) {
			REP( j, W + 1 ) {
				if( j - tre[i].second >= 0 ) {
					dp[i + 1][j] = max( dp[i][j], dp[i][j - tre[i].second] + tre[i].first );

					if( vmax < dp[i + 1][j] ) {
						vmax = dp[i + 1][j];
						w = j;
					} else if( vmax == dp[i + 1][j] ) {
						chmin( w, j );
					}


				} else {
					dp[i + 1][j] = dp[i][j];
				}
			}
		}

		cout << "Case " << ( cnt++ ) << ":" << endl;
		cout << dp[N][W] << endl << w << endl;

	}
}
