#include<iostream>
#include<vector>
#include<algorithm>
#include<cctype>
#include<utility>
#include<string>
#include<cmath>
#include<cstring>
#include<cstdio>

#define REP(i, n) for(int i = 0;i < n;i++)
#define REPR(i, n) for(int i = n;i >= 0;i--)
#define FOR(i, m, n) for(int i = m;i < n;i++)
#define FORR(i, m, n) for(int i = m;i >= n;i--)
#define SORT(v, n) sort(v, v+n);
#define VSORT(v) sort(v.begin(), v.end());
#define llong long long
#define pb(a) push_back(a)
#define INF 1000000000
using namespace std;
typedef pair<int, int> P;
typedef pair<llong, llong> LP;
typedef pair<int, P> PP;
typedef pair<llong, LP> LPP;




int main(){


    int W,N;
    char c;
    int times = 1;

    while(cin >> W){

        
        if(W == 0){
            return 0;
        }
        cin >> N;
        vector<int> v(N),w(N);
    
        REP(i,N){
            cin >> v[i] >> c >> w[i];
            //cout << v[i] << " " << w[i] << endl;
        }

        int dp[1010][1010];

        REP(i,1010){
            REP(j,1010){
                dp[i][j] = -INF;
            }
        }
        REP(i,1010){
            dp[0][i] = 0;
        }
        
        for(int i = 1;i <= N;i++){
            for(int j = 0;j <= W;j++){

                if(j >= w[i-1]){
                    dp[i][j] = max(dp[i-1][j],dp[i-1][j - w[i-1]] + v[i-1]  );
                }else{
                    dp[i][j] = dp[i-1][j];
                }
            }
        }
        int weight = 0;
        int value = 0;
        for(int i = 0;i <= W;i++){

            if(weight < dp[N][i]){
                weight = i-1;
                value = dp[N][i]; 
            }
        }

        printf("Case %d:\n%d\n%d\n",times,value,weight);


        times++;

    }
    


    return 0;

}
