#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
using namespace std;

#define rep(i, n) REP(i, 0, n)
#define ALL(v) v.begin(), v.end()
#define MSG(a) cout << #a << " " << a << endl;
#define REP(i, x, n) for (int i = x; i < n; i++)
#define OP(m) cout << m << endl
typedef long long ll;
typedef unsigned long long ull;

int main()
{
    int wight, n;
    int cnt = 0;
    while (cin >> wight, wight)
    {
        cin >> n;
        int v[n + 1], w[n + 1];
        char c;
        for (int i = 1; i <= n; i++)
            cin >> v[i] >> c >> w[i];

        int dp[n + 1][wight + 1];
        rep(i, n + 1) rep(j, wight + 1) dp[i][j] = 0;

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

        int m = dp[n][wight];
        cnt++;
        cout << "Case " << cnt << ":" << endl;
        OP(m);
        rep(i, wight + 1)
        {
            if (m == dp[n][i])
            {
                OP(i);
                break;
            }
        }
    }
    return 0;
}
