#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#define MAX_N 1000

using namespace std;

int W, N;
int maxValue = 0, maxWeight = 0;
int value[MAX_N], weight[MAX_N];

void dfs(int pos, int Wsum, int Vsum) {
    if(Wsum > W) return;
    maxValue = max(maxValue, Vsum);
    if(maxValue == Vsum) maxWeight = Wsum;
    if(pos >= N) return;
    
    dfs(pos + 1, Wsum + weight[pos], Vsum + value[pos]);
    dfs(pos + 1, Wsum, Vsum);
    return;
}

int main(void) {
    int no = 1;
    while(scanf("%d %d", &W, &N), W != 0) {
        for(int r = 0; r < N; r++) {
            scanf("%d,%d", &value[r], &weight[r]);
        }
        dfs(0, 0, 0);
        cout << "Case " << no++ << ":" << endl;
        cout << maxValue << endl << maxWeight << endl;
        maxValue = maxWeight = 0;
    }
    return(0);
}