#include <bits/stdc++.h>
using namespace std;
const long long N = 2 * 1e5 + 4;
long long add(long long a, long long b, long long m) {
  if (a < 0) a += m;
  if (b < 0) b += m;
  long long res = ((a % m) + (b % m)) % m;
  res %= m;
  return res;
}
long long mul(long long a, long long b, long long m) {
  if (a < 0) a += m;
  if (b < 0) b += m;
  long long res = ((a % m) * (b % m)) % m;
  res %= m;
  return res;
}
long long sub(long long a, long long b, long long m) {
  long long res = (a - b + m) % m;
  res %= m;
  return res;
}
int32_t main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  long long t;
  cin >> t;
  while (t--) {
    long long n, k;
    cin >> n >> k;
    string second;
    cin >> second;
    vector<long long> dp(n + 1, 1e12), pref(n + 1, 0);
    dp[0] = 0;
    for (long long i = 1; i <= n; i++) {
      if (second[i - 1] == '1') pref[i] = 1;
      pref[i] += pref[i - 1];
    }
    for (long long i = 1; i <= n; i++) {
      long long a = 0, b = 0;
      if (second[i - 1] == '0') a++, b++;
      long long prev = i - k;
      if (prev < 1)
        a += pref[i - 1];
      else
        a += dp[prev] + pref[i - 1] - pref[prev];
      b += pref[i - 1];
      dp[i] = min(a, b);
    }
    long long res = pref[n];
    for (long long i = 1; i <= n; i++) {
      res = min(res, dp[i] + pref[n] - pref[i]);
    }
    cout << res << '\n';
  }
  return 0;
}
