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

int W, N;

struct T {
    int v, w;
    T(int value, int weight) {
        v = value, w = weight;
    }
    bool operator<(const T &o) const {
        return (w > o.w);
    }
};

vector<T> ts;

void init() {
    ts.clear();
    W = N = 0;
}

bool get_input() {
    init();
    cin >> W;
    if (W == 0) return false;
    cin >> N;
    int value, weight;
    char dummy;
    for (int i = 0; i < N; i++) {
        cin >> value >> dummy >> weight;
        ts.push_back(T(value, weight));
    }
    sort(ts.begin(), ts.end());
    return true;
}

T vmax(T t1, T t2) {
    if (t1.v == t2.v) {
        if (t1.w > t2.w) {
            return t2;
        } else {
            return t1;
        }
    } else if (t1.v > t2.v) {
        return t1;
    } else {
        return t2;
    }
}

T solve(T t, int ptr) {
    if (ptr == ts.size()) return t;
    if (ts[ptr].w + t.w > 50) 
        return solve(t, ptr+1);
    else
        return vmax(solve(t, ptr+1), solve(T(t.v + ts[ptr].v, t.w + ts[ptr].w), ptr+1));
}

void format(T t, int data_set_n) {
    cout << "Case " << data_set_n << ':' << endl;
    cout << t.v << endl;
    cout << t.w << endl;
}


int main() {
    int dn = 1;
    while (get_input()) {
        format(solve(T(0, 0), 0), dn++);
    }
    return 0;
}