#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>

using namespace std;

int w[1001];
int v[1001];

int dp[1001][10001];

int W,N;

int solve(){
    for(int i=0; i<N; i++){
        for(int j=0; j<=W; j++){
            if(j - w[i] > 0){
                dp[i+1][j] = max(dp[i][j] , dp[i][j-w[i]] + v[i]);
            }else{
                dp[i+1][j] = dp[i][j];
            }
        }
    }
    return dp[N][W];
}

int main(){
    int t=1;
    while(cin >> W, W){
        memset(dp, 0, sizeof(dp));
        memset(w, 0, sizeof(w));
        memset(v, 0, sizeof(v));
        cin >> N;
        for(int i=0; i<N; i++){
            scanf("%d,%d",&v[i],&w[i]);
        }
        cout << "Case " << t++ << ":" << endl;
        cout << solve() << endl;

        int i = N, j = W;
        int wei = 0;
        while(i != 0){
            for(int l = 0; l*w[i-1] <= j; l++){
                int take = dp[i-1][j - l*w[i-1]] + l*v[i-1];
                if(take == dp[i][j]){
                    wei += l*w[i-1];
                    j -= l*w[i-1];
                    i--;
                    break;
                }
            }
        }
        cout << wei << endl;
    }
}