#include <iostream>
#include <vector>
#include <utility>

using namespace std;

typedef pair<int,int> ip;

int solve(int W, int N, vector<int>& v, vector<int>& w){
    vector< vector<ip> > dp(N+1, vector<ip>(W+1));
    for(int i = N - 1; i >= 0; i--){
        for(int j = 0; j <= W; j++){
            if(j < w[i]){
                dp[i][j] = dp[i+1][j];
            } else {
                int a = dp[i+1][j].first;
                int b = dp[i+1][j-w[i]].first + v[i];
                if(a >= b) dp[i][j] = dp[i+1][j];
                else {
                    dp[i][j] = make_pair(b, dp[i+1][j-w[i]].second + w[i]);
                }
            }
        }
    }
    cout << dp[0][W].first << endl;
    cout << dp[0][W].second << endl;
}

int main(){
    int W, N;
    char c;
    int cnt = 1;
    while(true){
        cin >> W;
        if(W == 0) break;
        cin >> N;
        vector<int> v(N);
        vector<int> w(N);
        for(int i=0; i<N; ++i){
            cin >> v[i] >> c >> w[i];
        }
        cout << "Case " << cnt++ << ":" << endl;
        solve(W,N,v,w);
    }
}