#include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
#include<deque>
#include<map>
#include<set>
#include<string>
#include<sstream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<cctype>
#include<climits>
using namespace std;
#define REP(i, j) for(int i = 0; i < j; i++)
#define FOR(i, j, k) for(int i = j; i < k; i++)
#define P pair<int, int>
const int INF = INT_MAX / 2;
const int MAX_N = 1000;

int w, n, ans_w = INF, ans = -1;

void loop(int now, int cnt, int sum_w, int sum, vector<P> j){
    if(now >= n){
        if(ans < sum){
            ans = sum;
            ans_w = sum_w;
        }
        else if(ans == sum){
            if(ans_w > sum_w){
                ans = sum;
                ans_w = sum_w;
            }
        }
    } else{
        loop(now + 1, cnt, sum_w, sum, j);
        if(sum_w + j[now].second <= w) loop(now + 1, cnt + 1, sum_w + j[now].second, sum + j[now].first, j);
    }
}

int main(){
    int c = 1;
    while(cin >>w && w){
        ans = -1;
        //input begin
        cin >>n;
        vector<P> j(n);
        string str;
        getline(cin, str);
        REP(i, n){
            getline(cin, str);
            stringstream ss(str);
            int a, b;
            char c;
            ss >>a >>c >>b;
            j[i].first = a;
            j[i].second = b;
        }
        //input end

        loop(0, 0, 0, 0, j);
        cout <<"Case " <<c <<":" <<endl <<ans <<endl <<ans_w <<endl;
        c++;

    }
    return 0;
}