#include <iostream>
#include <vector>
#include <list>
#include <algorithm>
#include <string>
#include <stack>
#include <iomanip>
#include <numeric>
#include <queue>
#include <climits>
#include <cmath>
using namespace std;
using ll = long long;
#define MOD 1000000007
#define INF 1LL << 59

int main()
{
    std::ios::sync_with_stdio(false);
    std::cin.tie(0);

    int W, N;
    int cnt = 0;
    while (cin >> W >> N)
    {
        cnt++;
        if (W == 0)
            break;

        vector<int> v(N), w(N);
        char c;
        for (int i = 0; i < N; ++i)
            cin >> v[i] >> c >> w[i];

        int dp[1010][1010];
        for (int i = 0; i <= N; ++i)
        {
            for (int j = 0; j <= W; ++j)
            {
                dp[i][j] = 0;
            }
        }

        for (int i = 0; i < N; ++i)
        {
            for (int j = 0; j <= W; ++j)
            {
                if (j - w[i] >= 0)
                {

                    dp[i + 1][j] = max(dp[i][j], dp[i][j - w[i]] + v[i]);
                }
                else
                {

                    dp[i + 1][j] = max(dp[i + 1][j], dp[i][j]);
                }
            }
        }
        cout << "Case " << cnt << ":" << endl;
        int mv = 0, mw = 0;

        for (int i = 0; i <= W; ++i)
        {
            if (dp[N][i] > mv)
            {
                mv = dp[N][i];
                mw = i;
            }
        }
        cout << mv << "\n"
             << mw << endl;
    }

    return 0;
}
