//dpoeschl
import java.util.*;

public class RoadA
{
    static int complete;
    static boolean window[]; 
    static int windowSize;

    public static void main(String[] args)
    {
        Scanner scan = new Scanner(System.in);
        int m = scan.nextInt();

        for(int c = 1; c <= m; c++)
        {
            complete = 0;
            window = new boolean[1000];

            int lines = scan.nextInt();
            windowSize = scan.nextInt();
            scan.nextLine();

            for(int i = 0; i < lines; i++)
                process(reduce(scan.nextLine()));

            int highest = complete;
            for(int i = 0; i < windowSize; i++)
                if(window[i])
                    highest = complete + i + 1;

            System.out.println("Case " + c + ": " + complete + " " + highest);
        }
    }

    public static List<String> reduce(String line)
    {
        List<String> result = new ArrayList<String>();
        char[] chars = line.toCharArray();
        StringBuffer sb = new StringBuffer();

        for(int i = 0; i < chars.length; i++)
        {
            if('0' <= chars[i] && chars[i] <= '9')
            {
                sb.append(chars[i]);
            }
            else if(sb.length() > 0)
            {
                result.add(sb.toString());
                sb = new StringBuffer();
            }
        }

        if(sb.length() > 0)
            result.add(sb.toString());

        return result;
    }

    public static void process(List<String> ints)
    {
        while(true)
        {
            int minDigits = Integer.toString(complete).length();
            int maxDigits = Integer.toString(complete + 1000).length();

            for(String str : ints)
            {
                int len = str.length();
                for(int digits = minDigits; digits <= maxDigits; digits++)
                {
                    for(int a = 0; a <= len - digits; a++)
                    {
                        int x = Integer.parseInt(str.substring(a, a+digits));
                        int offset = x - complete - 1;
                        if(0 <= offset && offset < 1000)
                            window[offset] = true;
                    }
                }
            }

            int newComplete = complete;
            for(int i = 0; i < 1000; i++)
            {
                if(window[i])
                    newComplete++;
                else
                    break;
            }

            if(newComplete > complete)
            {
                for(int i = 0; i < 1000; i++)
                    if(i < 1000 - newComplete + complete)
                        window[i] = window[i + newComplete - complete];
                    else
                        window[i] = false;

                complete = newComplete;
            }
            else
            {
                for(int i = windowSize; i < 1000; i++)
                    window[i] = false;
                
                return;
            }
        }
    }
}
