#include <bits/stdc++.h>
using namespace std;
const int N = 1000019;
int vis[N];
int a[N], b[N];
int n, k;
int c(int l, int r) {
  r++;
  l++;
  r = min(r, n);
  if (r < l) return 0;
  int x = b[r] - b[l - 1];
  return x;
}
int go(int x) {
  if (x >= n) return 0;
  if (vis[x] != -1) return vis[x];
  vis[x] = 0;
  vis[x] = go(x + k) + c(x + 1, x + k - 1);
  if (a[x] == 0) vis[x]++;
  vis[x] = min(vis[x], c(x, n - 1));
  return vis[x];
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  memset(vis, -1, sizeof vis);
  int t;
  cin >> t;
  while (t--) {
    cin >> n >> k;
    for (int i = 0; i < n; i++) {
      char c;
      cin >> c;
      a[i] = c - '0';
      b[i + 1] = a[i];
      b[i + 1] += b[i];
    }
    int mn = n;
    for (int i = 0; i < n; i++) {
      mn = min(mn, c(0, i - 1) + go(i));
    }
    cout << mn << endl;
    for (int i = 0; i < n + 3; i++) vis[i] = -1;
  }
}
