#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;

int n, w, maxValue, maxWeight;
const int MAX_N = 1000;
int treasures[MAX_N][2];

void dfs(int index, int value, int weight) {
    if (index == n) {
        if (maxValue < value) {
            maxValue = value;
            maxWeight = weight;
        }
        return;
    }
    if (weight + treasures[index][1] < w) {
        dfs(index + 1, value + treasures[index][0], weight + treasures[index][1]);
    }
    dfs(index + 1, value, weight);
}

int main() {
    int t = 0;
    while (true) {
        cin >> w >> n;
        if (w == 0) {
            break;
        }
        memset(treasures, 0, sizeof(treasures));
        for (int i = 0; i < n; i++) {
            scanf("%d,%d", treasures[i], treasures[i] + 1);
        }
        maxValue = 0;
        dfs(0, 0, 0);
        cout << "Case " << ++t << ":" << endl;
        cout << maxValue << endl << maxWeight << endl;
    }
}