// Volume0 - 0042: A Thief
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
 
//#define DEBUG
 
// ??????????????°??? 1000 ??\????????????????????????10000 ??\???
// ???????????????????????§??????10^7
class Solver {
private:
    int caseNum; // ???????????????????????????
    int w; // ?¢¨??????????????????????????????W(1000 ??\???)
    int n; // ???????????° N
    vector<int> vv; // vector of value
    vector<int> vw; // vector of weight
    int maxValue;  // ?¢¨???????????\???????????????????????????
    int maxWeight; // ????????¨??????????????????????????????
public:
    Solver():caseNum(0){}
    bool input();
    void calc();
    void output();
};
bool Solver::input() {
    ++caseNum;
    cin >> w;
    if(w == 0) return false;
    cin >> n;
    vv.clear();
    vw.clear();
    for(int i=0; i<n; i++) {
        int value, weight;
        char ch;
        cin >> value >> ch >> weight;
        vv.push_back(value);
        vw.push_back(weight);
    }
    return true;
}
void Solver::calc() {
    vector<int> memo(w+1,0);
    for(unsigned int i=0; i<vv.size(); i++) {
        vector<int> temp = memo;
        for(unsigned int j=0; j+vw[i]<=memo.size(); j++) {
            temp[j+vw[i]] = std::max(memo[j+vw[i]], memo[j]+vv[i]);
        }
        memo = temp;
    }
    maxValue = 0;
    maxWeight = 0;
    for(int i=memo.size()-1; i>=0; --i) {
        if(maxValue <= memo[i]) {
            maxValue = memo[i];
            maxWeight = i;
        }
    }
}
void Solver::output() {
    cout << "Case " << caseNum << ":" << endl;
    cout << maxValue << endl;
    cout << maxWeight << endl;
}
 
int main(int argc, char** argv) {
    Solver solver;
    while(solver.input()) {
        solver.calc();
        solver.output();
    }
    return 0;
}