#include <bits/stdc++.h>
using namespace std;
#define int lint
typedef long long lint;
typedef pair<int,int> P;
const int    INF=(int)1e9;
const int    MOD=(int)1e9+7;
const double EPS=(double)1e-10;
struct Accelerate_Cin{
    Accelerate_Cin(){
        cin.tie(0); ios::sync_with_stdio(0);cout<<fixed<<setprecision(20);
    };
};

queue<int>q;

bool calc(){
  int W;cin>>W;
  if(W==0)return true;
  int N;cin>>N;

  int v[1010],w[1010];
  for(int t=1;t<=N;t++){
    char c;
    cin>>v[t]>>c>>w[t];
  }
  static int dp[1010][1010];  //dp[n][w]:=n個目までから、総重量w以下で選んだ時の最大総和価値
  memset(dp,0,sizeof(dp));

  for(int i=0;i<N;i++){
    for(int j=0;j<=W;j++){

      if(w[i+1]>j){
        dp[i+1][j]=dp[i][j];
      }else{
        dp[i+1][j]=max(dp[i][j],dp[i][j-w[i+1]]+v[i+1]);
      }

    }
  }

  int ans_w;
  for(int w=0;w<=W;w++){
    if(dp[N][w]==dp[N][W]){
      ans_w=w;
      break;
    }
  }

  q.push(dp[N][W]);
  q.push(ans_w);
  return false;
}

signed main(){
  while(true){
    bool flag=calc();
    if(flag)break;
  }

  int t=1;
  while(!q.empty()){
    cout<<"Case "<<t<<":"<<"\n";
    cout<<q.front()<<"\n";q.pop();
    cout<<q.front()<<"\n";q.pop();
    t++;
  }
  return 0;
}

