#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <climits>
#include <cfloat>
#include <map>
#include <utility>
#include <set>
#include <iostream>
#include <memory>
#include <string>
#include <vector>
#include <algorithm>
#include <functional>
#include <sstream>
#include <fstream>
#include <complex>
#include <stack>
#include <queue>

using namespace std;
typedef long long LL;
typedef pair<int, int> P;
static const double EPS = 1e-5;

class Knapsack {
    struct item { 
        long long v, w; 
    };

    long long W;
    vector<item> items;
    long long lv;
    long long lv_w;

    pair<long long, long long> solve_rec(size_t k, long long v, long long w) {
        if (w + items[k].w > W) return solve_rec(k+1, v, w);
        long long cv = v, cw = w;
        for (size_t i = k; i < items.size(); ++i) {
            if (cw + items[i].w <= W) {
                cw += items[i].w;
                cv += items[i].v;
            }
        }
        if (lv < cv || (lv == cv && lv_w > cw)){
            lv = cv;
            lv_w = cw;
        }
        double fv = v, fw = w;
        for (size_t i = k; i < items.size(); ++i) {
            if (fw + items[i].w <= W) {
                fw += items[i].w;
                fv += items[i].v;
            } else {
                fv += items[i].v * (W - fw) / items[i].w;
                break;
            }
        }
        if (fv - lv < 1 || fv < lv) return make_pair(lv, lv_w);
        solve_rec(k+1, v+items[k].v, w+items[k].w);
        return solve_rec(k+1, v, w);
    }

    public:
    Knapsack() {}
    Knapsack(long long W_) : W(W_) {}
    void add_item(long long v, long long w) {
        items.push_back({v, w});
    }
    pair<long long, long long> solve() {
        sort(items.begin(), items.end(), [](const item &a, const item &b) { 
                return a.v * b.w > a.w * b.v;
                });
        lv = 0;
        return solve_rec(0, 0, 0);
    };
};

#define FOR(i,k,n) for (int i=(k); i<(int)(n); ++i)
#define REP(i,n) FOR(i,0,n)
int main(void){
    int W,N,v[1001],w[1001];
    int cas = 1;
    while(cin>>W){
        if(W==0) break;
        printf("Case %d:\n",cas++);
        Knapsack solver(W);
        cin>>N;
        REP(i,N){
            scanf("%d,%d",&v[i],&w[i]);
            solver.add_item(v[i], w[i]);
        }
        pair<long long, long long> p = solver.solve();
        printf("%lld\n%lld\n", p.first, p.second);
    }
    return 0;
}