// tom w import java.util.*; import java.io.*; public class RoadC { public static int maxComplete; public static int pos; public static int maxDigits = 10; public static void main(String[] args) throws FileNotFoundException { //long time = System.currentTimeMillis(); Scanner input = new Scanner(System.in); String line; int cases = input.nextInt(); input.nextLine(); int n; int wCap; int w = 1000; for(int c = 1; c <= cases; c++) { n = input.nextInt(); wCap = input.nextInt(); input.nextLine(); maxComplete = 0; int[] W = new int[w]; pos = 0; for(int i = 0; i < n; i++) { line = input.nextLine(); boolean shifted = true; while(shifted) { shifted = FillWindow(line, W); } for(int j = wCap; j < w; j++) { W[(j+pos)%w] = 0; } for(int j = 0; j < wCap; j++) { } } int lastVal = maxComplete; for(int i = 0; i < w; i++) { if(W[(pos+i)%w] == 1) { lastVal = maxComplete + 1 + i; } } System.out.println("Case "+c+": "+maxComplete+" "+lastVal); } //System.out.println("Run time: "+(System.currentTimeMillis()-time)); } public static int TenToThe(int x) { if (x == 0) return 1; else return 10*TenToThe(x-1); } public static boolean FillWindow(String line, int[] W) { int bufferLength = 0; int[] Buffer = new int[maxDigits]; boolean shifted = false; int windowMin = maxComplete+1; int windowMax = maxComplete+W.length; for(int currentIndex = 0; currentIndex < line.length(); currentIndex++) { int c = (int)line.charAt(currentIndex) - 48; if (0 <= c && c <= 9) { if(bufferLength == maxDigits) { for(int i = 0; i= windowMin) { if (W[(currentValue - maxComplete - 1 + pos)%W.length] == 1) { } else { W[(currentValue - maxComplete - 1 + pos)%W.length] = 1; } if(currentValue == maxComplete+1) {shifted = shifted || ShiftWindow(W); windowMin = maxComplete+1; windowMax = maxComplete+W.length; } } if(currentDigits < bufferLength) { currentValue = currentValue + Buffer[bufferLength - currentDigits-1]*TenToThe(currentDigits); } currentDigits++; } } else { bufferLength = 0; } } return shifted; } public static boolean ShiftWindow(int[] W) { boolean shift = false; while(W[pos] == 1) { shift = true; W[pos] = 0; pos = (pos+1)%W.length; maxComplete++; } return shift; } }