// Arup Guha
// 4/7/2017
// Solution to 2017 Code Jam Qualification Problem A: Oversized Pancake Flipper

import java.util.*;

public class a {

	public static void main(String[] args) {

		Scanner stdin = new Scanner(System.in);
		int numCases = stdin.nextInt();

		// Process each case.
		for (int loop=1; loop<=numCases; loop++) {

			// Store string as booleans of whether or not a pancake needs to be flipped.
			String s = stdin.next();
			int k = stdin.nextInt();
			boolean[] arr = get(s);

			int res = 0;
			
			// Try each flip in order; it's the last time you get to flip pancake i.
			for (int i=0; i<=s.length()-k; i++) {
				
				// If this one's wrong, we're forced to do this flip since we get no other chances, so do it.
				if (!arr[i]) {
					for (int j=i; j<i+k; j++)
						arr[j] = !arr[j];
					res++;
				}
			}

			// Now, when we get here, check if our pancakes are all the right side up.
			boolean ans = true;
			for (int i=0; i<arr.length; i++) {
				if (!arr[i]) {
					ans = false;
					break;
				}
			}

			// Answer accordingly.
			if (!ans)
				System.out.println("Case #"+loop+": IMPOSSIBLE");
			else
				System.out.println("Case #"+loop+": "+res);
		}
	}

	public static boolean[] get(String s) {
		boolean[] res = new boolean[s.length()];
		for (int i=0; i<s.length(); i++)
			res[i] = (s.charAt(i) == '+');
		return res;
	}
}