#include <iostream>
#include <algorithm>

using namespace std;

int main()
{

    int w, cas = 1;

    while (cin >> w, w) {
        int n, max_idx, max_value;
        int dp[1001][1001] = {0};
        int we[1001], v[1001];
        char ch;

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

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

        max_idx = max_value = 0;
        for (int j = 0; j <= w; j++)
            if (max_value < dp[n][j]) {
                max_value = dp[n][j];
                max_idx = j;
            }
        
        cout << "Case " << cas++ << ":\n" << max_value << '\n' << max_idx << endl;
    }

    return 0;
}