// Arup Guha
// 7/20/2013
// Solution to 2012 East Central Regional Problem F: Road Series

import java.util.*;

public class f {

	public static int window;
	public static int high;
	public static boolean[] found;

	public static void main(String[] args) {

		Scanner stdin = new Scanner(System.in);
		int numCases = Integer.parseInt(stdin.nextLine().trim());

		// Go through each case.
		for (int loop=1; loop<=numCases; loop++) {

			// Get problem specs.
			StringTokenizer tok = new StringTokenizer(stdin.nextLine());
			int numSigns = Integer.parseInt(tok.nextToken());
			window = Integer.parseInt(tok.nextToken());

			// Will store numbers seen. high stores first not seen.
			found = new boolean[1000000];
			high = 1;

			// Read through and update based on each sign.
			for (int i=0; i<numSigns; i++) {
				String line = stdin.nextLine();
				process(line);
			}

			System.out.println("Case "+loop+": "+(high-1)+" "+max());
		}

	}

	// Returns the largest index in found set to true.
	public static int max() {
		for (int i=found.length-1; i>=0; i--)
			if (found[i])
				return i;
		return 0;
	}

	public static void process(String line) {

		// First play complete game...
		while (canFind(line, high) || found[high]){
			found[high] = true;
			high++;
		}

		int max = high-1+window;

		// Fill in higher numbers...
		for (int i=high; i<=max; i++) {
			if (canFind(line, i)) {
				found[i] = true;
			}
		}

		// Update high, just in case it needs it.
		while (found[high]) high++;
	}

	public static boolean canFind(String s, int num) {
		String n = ""+num;
		return s.contains(n);
	}
}