#include <bits/stdc++.h>
using namespace std;
#define int              long long int
#define double           long double
#define print(a)         for(auto x : a) cout << x << " "; cout << endl


const int M = 1000000007;
const int N = 3e5+9;
const int INF = 2e9+1;
const int LINF = 2000000000000000001;

inline int power(int a, int b, int mod=M) {
    int x = 1;
    a %= mod;
    while (b) {
        if (b & 1) x = (x * a) % mod; 
        a = (a * a) % mod;
        b >>= 1;
    }
    return x;
}


//_ ***************************** START Below *******************************




vector<int> a;

//* TC = O(n)
//* SC = O(n)

int consistency1(int n, int k){

	int res = 0;
	
	vector<int> flipCt(n+1, 0);
	
	int countSoFar = 0;
	
	for(int i=0; i<n; i++){
		
	    int count = countSoFar;
	    if(i-k>=0) count = countSoFar - flipCt[i-k];
	    
	    int val = a[i];
	    if(count & 1)  val ^= 1;
	    
	    flipCt[i] = countSoFar;
	    
	    if(val == 0){
	        if(i+k-1 >= n) return -1;
	        flipCt[i]++;
	        countSoFar++;
	        res++;
	    }
	}
	return res;
}






//* TC = O(n)
//* SC = O(k)

int consistency2(int n, int k){

	int res = 0;
	
	queue<int> q;
	
	for(int i=0; i<n; i++){
		
	    while(!q.empty() && q.front() <= i-k) q.pop();
	    int count = q.size();
	    
	    int val = a[i];
	    if(count & 1)  val ^= 1;
	    
	    
	    if(val == 0){
	        if(i+k-1 >= n) return -1;
	        q.push(i);
	        res++;
	    }
	}
	return res;
}



//* TC = O(n)
//* SC = O(1) Inplace  (use a[] as prefix count)

int consistency3(int n, int k){

	int res = 0;
	
	int countSoFar = 0;
	
	for(int i=0; i<n; i++){
		
	    int count = countSoFar;
	    if(i-k>=0) count = countSoFar - a[i-k];
	    
	    int val = a[i];
	    if(count & 1)  val ^= 1;
	    
	    a[i] = countSoFar;
	    
	    if(val == 0){
	        if(i+k-1 >= n) return -1;
	        a[i]++;
	        countSoFar++;
	        res++;
	    }
	}
	return res;
}




















int practice(int n, int k){


    return 0;
}





void solve() {
    
    int n, k;
    cin>> n >> k;
    
    a.resize(n);
    for(int i=0; i<n; i++) cin >> a[i];
    
    cout << consistency2(n, k) << endl;


}





int32_t main() {
    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);

    int t = 1;
    cin >> t;
    while (t--) {
        solve();
    }

    return 0;
}