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

struct treasure_t{
    int value;
    int weight;
    treasure_t( const int v, const int w ){
        value = v;
        weight = w;
    }
};

vector<treasure_t> treasures;
int W, maxW, maxV;

void solve( int n, int w, int v ){
    if( w > W ) return;
    if( n==treasures.size() ){
        if( (v > maxV) || (v==maxV && w<maxW) ){
            maxV = v;
            maxW = w;
        }
        return;
    }
    solve(n+1,w+treasures[n].weight,v+treasures[n].value);
    solve(n+1,w,v);
}

int main(){
    int cnt = 1;
    while( cin >> W, W ){
        int n;
        cin >> n;
        treasures.clear();
        maxW = 0;
        maxV = 0;
        for( int i=0; i<n; i++ ){
            int v, w;
            scanf("%d,%d", &v,&w);
            treasure_t t(v,w);
            treasures.push_back(t);
        }
        solve(0,0,0);
        cout << "Case " << cnt << ":" << endl;
        cout << maxV << endl;
        cout << maxW << endl;
        cnt++;
    }
    return 0;
}