#include <iostream>
#include <vector>
#include <utility>
#define rep(i,k,n) for(int i=k;i<n;i++)
using namespace std;
typedef pair<int,int> takara;
//cost,weight
class napsack{
private:
  vector<takara> pack;
  int lim;
public:
  napsack(){
  };
  void push(takara a){
    takara tmp;
    int flag;
    int forend=pack.size();
    rep(i,0,forend){
      tmp=make_pair(a.first+pack[i].first,a.second+pack[i].second);
      // cout<<tmp.first<<" "<<tmp.second<<endl;  
      if(tmp.second>=lim) continue;
      flag=1;
     
      rep(j,0,forend)
	{
	if(pack[j].second>=tmp.second)
	  if(pack[j].first<tmp.first){
	    if(flag){flag--;
	      pack[j]=tmp;}
	    else{ pack.erase(pack.begin()+j);
	      forend--;}
	    }
      }
  
      rep(j,0,pack.size())
	if(pack[j].first>=tmp.first) {flag=355;break;}
      if(flag!=355) pack.push_back(tmp);
    }
  }
  void test(){
    rep(i,0,pack.size()){
      cout<<pack[i].first<<" "<<pack[i].second<<endl;
    }
  }
  void max(int uuu){
    takara tmp=make_pair(0,0);
    rep(i,0,pack.size()){
      if(tmp.first==pack[i].first)
	if(tmp.second>pack[i].second)
	  tmp=pack[i];
      if(tmp.first<pack[i].first)
	tmp=pack[i];	
    }
    cout <<"case "<<uuu<<":"<<endl<<tmp.first<<endl<<tmp.second<<endl;
  }
  void reset(int i){
    lim=i;
    pack.resize(1,make_pair(0,0));
  }

};

 
int main(){
  int w,n,a,b,count;
  napsack sack;
  char th;  
  count=0;
  while(1){
    count++;
    cin >>w;
    if(w==0) break;
    cin >>n;
    sack.reset(w);
    rep(i,0,n){
      cin>>a>>th>>b;
      sack.push(make_pair(a,b));
    }
    // sack.test();
    sack.max(count);
  }
  return 0;
}
    