#include<cstdio>
#include<algorithm>
using namespace std;

int main(){
  int n,v[1000],w[1000],W,casenum = 0;
  int dp[1001][1001];
  const int INF = 100000000;
  
  while(scanf("%d",&W),W){
    casenum++;
    scanf("%d",&n);
    for(int i=0;i<n;i++)scanf("%d,%d",&v[i],&w[i]);
    
    for(int i=0;i<=W;i++)dp[0][i] = 0;

    int ansv = 0, answ = 0;
    for(int i=0;i<n;i++){
      for(int j=0;j<=W;j++){
	dp[i+1][j] = dp[i][j];
	if(j>=w[i] && dp[i+1][j] < dp[i][j-w[i]] + v[i]){
	  dp[i+1][j] = dp[i][j-w[i]] + v[i];
	  if( ansv < dp[i+1][j] || (ansv == dp[i+1][j] && answ > j) ){
	    ansv = dp[i+1][j]; answ = j;
	  }
	}
      }
    }
    
    printf("Case %d:\n",casenum);
    printf("%d\n%d\n",ansv,answ);
  }
}