//AOJ0042.cpp created 8 28, 2015

#include <iostream>
#include <algorithm>
#include <cstdio>
const int MAX_N = 1000;
const int MAX_W = 1000;

struct status{
    int weight;
    int value;
}st[MAX_N];

struct status ini={-1,-1};
struct status dp[MAX_N+1][MAX_W+1];
int n,stweight;

struct status val(int i,int j){
    if(dp[i][j].value!=-1) return dp[i][j];
    status retv;
    if(i==n){
        retv.value=0;
        retv.weight=0;
    }else if(j<st[i].weight){
        retv=val(i+1,j);
    }else{
        status add,nadd;
        nadd = val(i+1,j);
        add = val(i+1,j-st[i].weight);
        add.value +=st[i].value;
        retv.value = std::max(add.value,nadd.value);
        //retv=std::max(val(i+1,j),val(i+1,j-st[i].weight)+st[i].value);
        if(add.value == nadd.value) retv.weight=std::min(add.weight+st[i].weight,nadd.weight);
        else if(add.value>nadd.value) retv.weight = add.weight+st[i].weight;
        else retv.weight = nadd.weight;
    }
    return dp[i][j]=retv;
}

int main() {
    int no=1;
    while(std::cin >> stweight,stweight!=0){
        std::cin >> n;
        for(int i=0;i<MAX_N;i++)
            for(int j=0;j<MAX_W;j++)
                dp[i][j]=ini;
        for(int i=0;i<n;i++){
            scanf("%d,%d",&(st[i].value),&(st[i].weight));
        }
        struct status ans = val(0,stweight);
        std::cout <<"Case "<< no++ << ":" << std::endl << ans.value << std::endl << ans.weight << std::endl;
        
    }
    return 0;
}