#include <iostream>
#include <algorithm>
#include <string.h>
#include <queue>
#include <cstdio>
#define rep(i,k,n) for(int i = k ; i < n ; i++ )
#define INF 10000000
int v[1001],w[1001];
int dp[1001][1001];

using namespace std;

int main(){
  int W,n;
  int cas = 1;
  while(cin >> W ,W){
    fill(v,v+1001,0);
    fill(w,w+1001,0);
    fill(dp[0],dp[1001],0);
	 cin >> n;
	 char c;
	 for(int i = 0 ; i < n ; i++ ){
	   cin >> v[i] >> c >> w[i];
	 }
	 for(int i = 0 ; i < n ; i++ ){
	   for(int j = 0 ; j <= W ; j++ ){
	     if(j < w[i]){
	       dp[i+1][j] = dp[i][j]; 
	     } else {
	       dp[i+1][j] = max(dp[i][j],dp[i][j - w[i]] + v[i]);
	     }
	   }
	 }
	 int wight = 0,mx = 0;
	 for(int i = 0 ; i <= W ; i++ )if(dp[n][i] > mx)mx = dp[n][i],wight = i;
	 cout << "Case " << cas++ << ":"<<endl;
	 cout << dp[n][W] << endl;
	 cout << wight << endl;
	 }
      return 0;
  }