#include <bits/stdc++.h>
using namespace std;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
int ac[1123456];
int n, k;
string s;
int qtt(int l, int r) {
  r = min(r, n - 1);
  if (l > r) return 0;
  if (l - 1 >= 0)
    return ac[r] - ac[l - 1];
  else
    return ac[r];
}
int dp[1123456][2];
int go(int i, int o) {
  if (i >= n) return 0;
  int& ret = dp[i][o];
  if (ret != -1) return ret;
  if (o) {
    ret = min(go(i + k, 1) + qtt(i + 1, i + k - 1) + (s[i] == '0'),
              (s[i] == '0') + qtt(i + 1, n - 1));
  } else {
    ret = min(go(i + 1, 0) + (s[i] == '1'), (s[i] == '0') + qtt(i + 1, n - 1));
    ret = min(ret, go(i + k, 1) + (s[i] == '0') + qtt(i + 1, i + k - 1));
  }
  return ret;
}
int main(void) {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  int t;
  cin >> t;
  while (t--) {
    cin >> n >> k;
    cin >> s;
    ac[0] = (s[0] == '1');
    for (int i = 1; i < n; i++) {
      ac[i] = ac[i - 1] + (s[i] == '1');
    }
    for (int i = 0; i <= n; i++) dp[i][0] = dp[i][1] = -1;
    int ans = min(go(0, 0), go(0, 1));
    cout << ans << "\n";
  }
  return 0;
}
