#include <iostream>
#include <cstring>
#include <vector>

using namespace std;

int blah[100000000];
int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);

    memset(blah, 0, sizeof blah);

    int ncase; cin >> ncase;
    for (int csnum = 1; csnum <= ncase; csnum++) {
        int k, w; cin >> k >> w;
        string tmp; getline(cin, tmp);

        int n = 0;
        blah[0] = csnum;
        vector<int> npos;
        for (int i = 0; i < k; i++) {
            string line;
            getline(cin, line);
            npos.resize(0);
            for (int j = 0; j < line.length(); j++) {
                if ('1' <= line[j] && line[j] <= '9') {
                    npos.push_back(j);
                }
            }
            bool done = false;
            while (!done) {
                done = true;
                for (int jj = 0; jj < npos.size(); jj++) {
                    int j = npos[jj];
                    int z = 0;
                    for (int k = j; k < line.length(); k++) {
                        if ('0' <= line[k] && line[k] <= '9') {
                            z = z*10 + (int)(line[k] - '0');
                            if (z > n+w) break;
                            if (n <= z && z <= n+w && blah[z] != csnum) {
                                blah[z] = csnum;
                                while (blah[n+1] == csnum) n++;
                                done = false;
                            }
                        } else {
                            break;
                        }
                    }
                }
            }
        }
        int nn = n+w;
        while (blah[nn] != csnum) nn--;
        cout << "Case " << csnum << ": " << n << " " << nn << endl;
    }
}