#include <iostream>
#include <algorithm>

using namespace std;

const int NUM_MAX = 1001;
int dp[NUM_MAX][NUM_MAX];
int we[NUM_MAX], v[NUM_MAX];
int w, n;

void solve()
{
    int max_idx;

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

    max_idx = 0;
    for (int j = 0; j <= w; j++)
        if (dp[n][j] == dp[n][w]) {
            max_idx = j;
            break;
        }
        
    cout << dp[n][w] << '\n' << max_idx << endl;
}

int main()
{
    int cas = 1;

    while (cin >> w, w) {
        char ch;

        cin >> n;
        for (int i = 0; i < n; i++)
            cin >> v[i] >> ch >> we[i];

        for (int i = 0; i <= n; i++)
            for (int j = 0; j <= w; j++)
                dp[i][j] = 0;

        cout << "Case " << cas++ << ":\n";
        solve();
    }

    return 0;
}