#include <bits/stdc++.h>
using namespace std;
const int MAXN = 2e6 + 7;
const int ALPHA = 40;
const long long INF = 1e16 + 7;
const int MOD = 1e9 + 7;
const int LOG = 22;
const int BASE[2] = {313, 239};
long long n, d;
bool ns[MAXN], seen[MAXN];
vector<long long> group[MAXN], plast[MAXN];
void solve() {
  cin >> n >> d;
  for (int i = 0; i < n; i++) {
    group[i].clear();
    plast[i].clear();
  }
  for (int i = 0; i < n; i++) cin >> ns[i];
  fill(seen, seen + n, 0);
  long long ptr = 0, cnt = 0;
  for (int i = 0; i < n; i++) {
    ptr = i;
    if (seen[ptr]) continue;
    while (!seen[ptr]) {
      seen[ptr] = 1;
      group[cnt].push_back(ns[ptr]);
      ptr = (ptr + d) % n;
    }
    cnt++;
  }
  for (int i = 0; i < cnt; i++) {
    long long k = group[i].size();
    for (int j = 0; j < k; j++) group[i].push_back(group[i][j]);
    long long last = -INF;
    for (int j = 0; j < group[i].size(); j++) {
      if (!group[i][j]) last = j;
      plast[i].push_back(j - last);
    }
  }
  long long maxim = 0;
  for (int i = 0; i < cnt; i++) {
    for (int j = 0; j < group[i].size() / 2; j++) {
      maxim = max(maxim, min(plast[i][j], plast[i][j + (group[i].size() / 2)]));
    }
  }
  if (maxim >= INF)
    cout << -1 << '\n';
  else
    cout << maxim << '\n';
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  ;
  int t;
  cin >> t;
  while (t--) solve();
}

### Prompt

Generate a cpp solution to the following problem:

You are given an array a[0 … n - 1] = [a_0, a_1, …, a_{n - 1}] of zeroes and ones only. Note that in this problem, unlike the others, the array indexes are numbered from zero, not from one.

In one step, the array a is replaced by another array of length n according to the following rules: 

  1. First, a new array a^{→ d} is defined as a cyclic shift of the array a to the right by d cells. The elements of this array can be defined as a^{→ d}_i = a_{(i + n - d) mod n}, where (i + n - d) mod n is the remainder of integer division of i + n - d by n. 

It means that the whole array a^{→ d} can be represented as a sequence $$$a^{→ d} = [a_{n - d}, a_{n - d + 1}, …, a_{n - 1}, a_0, a_1, …, a_{n - d - 1}]$$$

  2. Then each element of the array a_i is replaced by a_i  \&  a^{→ d}_i, where \& is a logical "AND" operator. 



For example, if a = [0, 0, 1, 1] and d = 1, then a^{→ d} = [1, 0, 0, 1] and the value of a after the first step will be [0  \&  1, 0  \&  0, 1  \&  0, 1  \&  1], that is [0, 0, 0, 1].

The process ends when the array stops changing. For a given array a, determine whether it will consist of only zeros at the end of the process. If yes, also find the number of steps the process will take before it finishes.

Input

The first line contains an integer t (1 ≤ t ≤ 1000) — the number of test cases.

The next 2t lines contain descriptions of the test cases. 

The first line of each test case description contains two integers: n (1 ≤ n ≤ 10^6) — array size and d (1 ≤ d ≤ n) — cyclic shift offset. The second line of the description contains n space-separated integers a_i (0 ≤ a_i ≤ 1) — elements of the array.

It is guaranteed that the sum of n over all test cases does not exceed 10^6.

Output

Print t lines, each line containing the answer to the corresponding test case. The answer to a test case should be a single integer — the number of steps after which the array will contain only zeros for the first time. If there are still elements equal to 1 in the array after the end of the process, print -1.

Example

Input


5
2 1
0 1
3 2
0 1 0
5 2
1 1 0 1 0
4 2
0 1 0 1
1 1
0


Output


1
1
3
-1
0

Note

In the third sample test case the array will change as follows: 

  1. At the beginning a = [1, 1, 0, 1, 0], and a^{→ 2} = [1, 0, 1, 1, 0]. Their element-by-element "AND" is equal to $$$[1  \&  1, 1  \&  0, 0  \&  1, 1  \&  1, 0  \&  0] = [1, 0, 0, 1, 0]$$$ 
  2. Now a = [1, 0, 0, 1, 0], then a^{→ 2} = [1, 0, 1, 0, 0]. Their element-by-element "AND" equals to $$$[1  \&  1, 0  \&  0, 0  \&  1, 1  \&  0, 0  \&  0] = [1, 0, 0, 0, 0]$$$ 
  3. And finally, when a = [1, 0, 0, 0, 0] we get a^{→ 2} = [0, 0, 1, 0, 0]. Their element-by-element "AND" equals to $$$[1  \&  0, 0  \&  0, 0  \&  1, 0  \&  0, 0  \&  0] = [0, 0, 0, 0, 0]$$$ 

Thus, the answer is 3 steps.

In the fourth sample test case, the array will not change as it shifts by 2 to the right, so each element will be calculated as 0  \&  0 or 1  \&  1 thus not changing its value. So the answer is -1, the array will never contain only zeros.

### Response

```cpp
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 2e6 + 7;
const int ALPHA = 40;
const long long INF = 1e16 + 7;
const int MOD = 1e9 + 7;
const int LOG = 22;
const int BASE[2] = {313, 239};
long long n, d;
bool ns[MAXN], seen[MAXN];
vector<long long> group[MAXN], plast[MAXN];
void solve() {
  cin >> n >> d;
  for (int i = 0; i < n; i++) {
    group[i].clear();
    plast[i].clear();
  }
  for (int i = 0; i < n; i++) cin >> ns[i];
  fill(seen, seen + n, 0);
  long long ptr = 0, cnt = 0;
  for (int i = 0; i < n; i++) {
    ptr = i;
    if (seen[ptr]) continue;
    while (!seen[ptr]) {
      seen[ptr] = 1;
      group[cnt].push_back(ns[ptr]);
      ptr = (ptr + d) % n;
    }
    cnt++;
  }
  for (int i = 0; i < cnt; i++) {
    long long k = group[i].size();
    for (int j = 0; j < k; j++) group[i].push_back(group[i][j]);
    long long last = -INF;
    for (int j = 0; j < group[i].size(); j++) {
      if (!group[i][j]) last = j;
      plast[i].push_back(j - last);
    }
  }
  long long maxim = 0;
  for (int i = 0; i < cnt; i++) {
    for (int j = 0; j < group[i].size() / 2; j++) {
      maxim = max(maxim, min(plast[i][j], plast[i][j + (group[i].size() / 2)]));
    }
  }
  if (maxim >= INF)
    cout << -1 << '\n';
  else
    cout << maxim << '\n';
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  ;
  int t;
  cin >> t;
  while (t--) solve();
}

```