CF BUDDY
← Problems·

1979D · Fixing a Binary String

1800 · bitmasks, brute force, constructive algorithms

Problem: You are given a binary string ss of length nn, consisting of zeros and ones. You can perform the following operation exactly once:

  1. Choose an integer pp (1pn1 \le p \le n).
  2. Reverse the substring s1s2sps_1 s_2 \ldots s_p. After this step, the string s1s2sns_1 s_2 \ldots s_n will become spsp1s1sp+1sp+2sns_p s_{p-1} \ldots s_1 s_{p+1} s_{p+2} \ldots s_n.
  3. Then, perform a cyclic shift of the string ss to the left pp times. After this step, the initial string s1s2sns_1s_2 \ldots s_n will become sp+1sp+2snspsp1s1s_{p+1}s_{p+2} \ldots s_n s_p s_{p-1} \ldots s_1.

For example, if you apply the operation to the string 110001100110 with p=3p=3, after the second step, the string will become 011001100110, and after the third step, it will become 001100110011.

A string ss is called kk-proper if two conditions are met:

  • s1=s2==sks_1=s_2=\ldots=s_k;
  • si+ksis_{i+k} \neq s_i for any ii (1ink1 \le i \le n - k).

For example, with k=3k=3, the strings 000, 111000111, and 111000 are kk-proper, while the strings 000000, 001100, and 1110000 are not.

You are given an integer kk, which is a divisor of nn. Find an integer pp (1pn1 \le p \le n) such that after performing the operation, the string ss becomes kk-proper, or determine that it is impossible. Note that if the string is initially kk-proper, you still need to apply exactly one operation to it.

Input Format: Each test consists of multiple test cases. The first line contains one integer tt (1t1041 \le t \le 10^4) — the number of test cases. The description of the test cases follows.

The first line of each test case contains two integers nn and kk (1kn1 \le k \le n, 2n1052 \le n \le 10^5) — the length of the string ss and the value of kk. It is guaranteed that kk is a divisor of nn.

The second line of each test case contains a binary string ss of length nn, consisting of the characters 0 and 1.

It is guaranteed that the sum of nn over all test cases does not exceed 21052 \cdot 10^5.

Output Format: For each test case, output a single integer — the value of pp to make the string kk-proper, or 1-1 if it is impossible.

If there are multiple solutions, output any of them.

Note: In the first test case, if you apply the operation with p=3p=3, after the second step of the operation, the string becomes 11100001, and after the third step, it becomes 00001111. This string is 44-proper.

In the second test case, it can be shown that there is no operation after which the string becomes 22-proper.

In the third test case, if you apply the operation with p=7p=7, after the second step of the operation, the string becomes 100011100011, and after the third step, it becomes 000111000111. This string is 33-proper.

In the fourth test case, after the operation with any pp, the string becomes 55-proper.

Sample Cases

Case 1

Input

7
8 4
11100001
4 2
1110
12 3
111000100011
5 5
00000
6 1
101001
8 4
01110001
12 2
110001100110

Output

3
-1
7
5
4
-1
3

Similar problems

00:00:00
Loading editor…
Welcome! I'm your coding tutor for this problem. Use the chips below to reveal stored hints or get AI feedback on your code. I'll guide you step by step — never giving away the solution.

Sign in to unlock AI tutor feedback