#include<bits/stdc++.h>
using namespace std;
vector<int> wmax;
int vmax;int n;
void dfs(vector<pair<int,int> > pii,int depth,int v,int w){
 
  if(w>n){
    return;
  }else if(v==vmax){wmax.push_back(w);
  }else if(v>vmax){wmax.clear();wmax.push_back(w);vmax=v;}
  if(pii.size()==depth+1)
    return;
  //cout<<pii[depth].first<<" "<<pii[depth].second<<endl;
  dfs(pii,depth+1,v+pii[depth].first,w+pii[depth].second);
  dfs(pii,depth+1,v,w);
  
}
int main(){
  int dataset=1;
  while(cin>>n,n){
    wmax.clear();vmax=0;
    int num;cin>>num;
    vector<pair<int,int> > pii;
    for(int i=0;i<num;i++){
      int a,b;
      scanf("%d,%d",&a,&b);
      if(b<n){
	pii.push_back(make_pair(a,b));
	//cout<<pii[pii.size()-1].first<<" "<<pii[pii.size()-1].second<<endl;
      }
    }
    pii.push_back(make_pair(0,0));
    dfs(pii,0,0,0);
    int minw=1000000000;
    //cout<<wmax.size()<<endl;
    for(int i=0;i<wmax.size();i++){
      cout<<wmax[i]<<endl;
      if(minw>wmax[i])
	minw=wmax[i];
    }
    cout<<"Case "<<dataset<<":"<<endl;
    cout<<vmax<<endl;
    cout<<minw<<endl;
    dataset++;
  }
  return 0;
}