#include<bits/stdc++.h>
#define REP(i,n) for(int i=0;i<n;i++)
#define LOOP(i,x,n) for(int i=x;i<n;i++)
#define ALL(v) (v).begin(),(v).end()
#define PB push_back
#define MP make_pair
#define FR first
#define SC second
#define int long long
using namespace std;
const int MOD=1000000007;
const int INF=9000000009;

int dp[10004][10004];

signed main(){
  int W,N;
  int c=1;
  while(cin>>W>>N,W){
    vector<int> v(N),w(N);
    char a;
    REP(i,N)cin>>v[i]>>a>>w[i];
    dp[0][w[0]]=v[0];
    LOOP(i,1,N){
      REP(j,W){
        dp[i][j]=dp[i-1][j];
        if(j-w[i]>=0)dp[i][j]=max(dp[i][j],dp[i-1][j-w[i]]+v[i]);
      }
    }
    int ans=-INF,wei;
    REP(i,W){
      if(ans<dp[N-1][i]){
        ans=dp[N-1][i];
        wei=i;
      }
    }
    c++;
    cout<<"Case "<<c<<":"<<endl;
    cout<<ans<<endl;
    cout<<wei<<endl;
  }
  return 0;
}

