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

const int MAXW = 1000;
const int MAXN = 1000;
int DPV[MAXN + 4][MAXW + 4];
int DPW[MAXN + 4][MAXW + 4];
int W[MAXN + 4], V[MAXN + 4];

void init() {
  for (int i = 0; i < MAXN + 4; i++)
    W[i] = V[i] = 0;

  for (int i = 0; i < MAXN + 4; i++)
    for (int j = 0; j < MAXN + 4; j++)
      DPV[i][j] = DPW[i][j] = 0;
}

void solveV(int n, int w) {
  for (int i = n; i > 0; i--) {
    for (int j = 0; j <= w; j++) {
      if (j < W[i]) {
        DPV[i][j] = DPV[i + 1][j];
        DPW[i][j] = DPW[i + 1][j];
      } else {
        if (DPV[i + 1][j] > DPV[i + 1][j - W[i]]  + V[i]) {
          DPV[i][j] = DPV[i + 1][j];
          DPW[i][j] = DPW[i + 1][j];
        } else if (DPV[i + 1][j] == DPV[i + 1][j - W[i]] + V[i]) {
          DPV[i][j] = DPV[i + 1][j];
          DPW[i][j] = min(DPW[i + 1][j], DPW[i + 1][j - W[i]] + W[i]);
        } else {
          DPV[i][j] = DPV[i + 1][j - W[i]] + V[i];
          DPW[i][j] = DPW[i + 1][j - W[i]] + W[i];
        }
      }
    }
  }
}

int main() {
  int w, Case = 1;
  while (cin >> w and w != 0) {
    init();

    int n;
    cin >> n;
    for (int i = 1; i <= n; i++)
      scanf("%d,%d", &V[i], &W[i]);

    solveV(n, w);

    cout << "Case " << Case << ":" << endl;
    cout << DPV[1][w] << endl << DPW[1][w] << endl;

    Case++;
  }
}