#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;

#define SIZE 1001
#define MAX_W 1001

int main(void) {
    int V[SIZE][MAX_W] = {};
    int v[SIZE];
    int w[SIZE];
    int W, N;
    int ans_v, ans_w;
    int n = 1;
    
    while(true) {
        cin >> W;
        if(W == 0)   break;
        
        cin >> N;
        for(int i = 0; i < N; ++i) {
            scanf("%d,%d", &v[i], &w[i]);
        }
        
        for(int i = 1; i <= N; ++i) {
            for(int j = 1; j <= W; ++j) {
                if(w[i - 1] > j) {
                    V[i][j] = V[i - 1][j];
                } else {
                    V[i][j] = max(V[i - 1][j], V[i - 1][j - w[i - 1]] + v[i - 1]);
                }
            }
        }
        
        ans_w = W;
        while(V[N][ans_w] == V[N][W]) {
            --ans_w;
        }
        ++ans_w;
        ans_v = V[N][ans_w];
        
        printf("Case %d:\n", n);
        cout << ans_v << endl << ans_w << endl;
        
        ++n;
    }
    
    return 0;
}
