#include<iostream>
#include<cstdio>
#include<string>
#include<vector>
#include<map>

using namespace std;

struct Treasure
{

  long value;
  long weight;
  bool flag;

};


map<int,Treasure> memo;

bool flag;
long limit_weight;
long treasure_number;
Treasure result_treasure;

int AThief(vector<Treasure> &treasure,long &total_value,long &total_weight)
{

  if(limit_weight < total_weight){
    return 0;
  }
  else
    {

      for(int i = 0; i < treasure_number; i++)
        {

          if(treasure[i].flag)
            {

              total_value += treasure[i].value;
              total_weight += treasure[i].weight;
              treasure[i].flag = false;

              if(AThief(treasure,total_value,total_weight) == 0)
                {

                  total_value -= treasure[i].value;
                  total_weight -= treasure[i].weight;

                  if(result_treasure.value == total_value && result_treasure.weight > total_weight) result_treasure.weight = total_weight;
                  else if(result_treasure.value < total_value)
                    {
                      result_treasure.value = total_value;
                      result_treasure.weight = total_weight;
                    }
                  flag = false;
                }
              else
                {
                  total_value -= treasure[i].value;
                  total_weight -= treasure[i].weight;
                }

              treasure[i].flag = true;

            }

        }

    }

  //全部足しても耐えられる重さを越えなかった時
  if(flag)
    {
      result_treasure.value = total_value;
      result_treasure.weight = total_weight;
      flag = false;
    }


  return 1;

}


int main()
{

  int dummy;
  Treasure temp;
  int case_number = 0;
  long total_value = 0;
  long total_weight = 0;
  vector<Treasure> treasure;

  scanf("%d",&limit_weight);

  while(limit_weight!=0)
    {

      scanf("%d",&treasure_number);

      treasure.clear();

      for(int i = 0; i < treasure_number; i++)
        {

          scanf("%d,%d",&temp.value,&temp.weight);
          temp.flag = true;
          treasure.push_back(temp);

        }

      result_treasure.value = -1;
      result_treasure.weight = 2147483647;
      flag = true;

      dummy = AThief(treasure,total_value,total_weight);

      case_number++;

      cout << "Case " << case_number << ":" << endl;
      cout << result_treasure.value << endl;
      cout << result_treasure.weight << endl;

      cin >> limit_weight;

    }

}