#include<iostream>
using namespace std;

void sort( int* a, int* b, int n )
{
    for( int i = 0; i < n - 1; i++ )
        for( int j = i + 1; j < n; j++ )
            if( a[i] > a[j] )
            {
                int temp = a[i];
                a[i] = a[j];
                a[j] = temp;

                temp = b[i];
                b[i] = b[j];
                b[j] = temp;
            }
}

int main()
{
    int w, n, cnt = 0;
    char c;

    while( cin >> w )
    {
        if( w == 0 )
            break;

        cin >> n;

        cnt++;

        int val[1000], we[1000];

        for( int i = 0; i < n; i++ )
            cin >> val[i] >> c >> we[i];

        sort( we, val, n );

        int sum1 = 0, sum2 = 0, max1 = 0, max2 = 0, k = 1;
        for( int i = 0; i < n - 1; i++ )
        {
            sum1 = we[i];
            sum2 = val[i];
            for( int j = i + k; j < n; j++ )
            {
                if( sum1 + we[j] <= w )
                {
                    sum1 += we[j];
                    sum2 += val[j];

                    if( sum2 > max2 )
                    {
                        max2 = sum2;
                        max1 = sum1;
                    }
                    else if( sum2 == max2 )
                    {
                        if( sum1 < max1 )
                        {
                            max2 = sum2;
                            max1 = sum1;
                        }
                    }
                    else;
                }
                else
                {
                    if( sum2 > max2 )
                    {
                        max2 = sum2;
                        max1 = sum1;
                    }
                    else if( sum2 == max2 )
                    {
                        if( sum1 < max1 )
                        {
                            max2 = sum2;
                            max1 = sum1;
                        }
                        else
                        {
                            sum1 = we[i];
                            sum2 = val[i];
                            k++;
                            j = i + k;
                        }   
                    }
                    else
                    {
                        sum1 = we[i];
                        sum2 = val[i];
                        k++;
                        j = i + k;
                    }
                }
            }
            //cout << "sum1: " << sum1 << " sum2: " << sum2 << endl;
            //cout << "max1: " << max1 << " max2; " << max2 << endl;
        }
        cout << "Case " << cnt << ":" << endl << max2 << endl << max1 << endl;

    }

    return 0;
}
