#include <bits/stdc++.h>
using namespace std;
template <typename T, typename U>
inline void smin(T &a, const U &b) {
  if (a > b) a = b;
}
template <typename T, typename U>
inline void smax(T &a, const U &b) {
  if (a < b) a = b;
}
const int NN = 1000100;
int b[NN], dp[NN], s[NN];
char a[NN];
int main() {
  int t;
  scanf("%d", &t);
  while (t--) {
    int n, k;
    scanf("%d%d", &n, &k);
    scanf("%s", a + 1);
    int ans = 0;
    for (int i = 1; i <= n; i++)
      if (a[i] == '1') ans++;
    for (int i = 1; i <= n; i++) {
      s[i] = s[i - 1];
      if (a[i] == '1') s[i]++;
    }
    for (int i = 1; i <= n; i++) {
      if (i <= k) {
        if (a[i] == '1') dp[i] = s[n] - 1;
        if (a[i] == '0') dp[i] = s[n];
        ans = min(ans, dp[i]);
        continue;
      }
      if (a[i] == '1') {
        if (a[i - k] == '1') {
          dp[i] = dp[i - k] - 1;
        } else {
          dp[i] = min(dp[i - k] - 1, s[n] - 1);
        }
      }
      if (a[i] == '0') {
        if (a[i - k] == '1') {
          dp[i] = dp[i - k] + 1;
        } else {
          dp[i] = min(dp[i - k] + 1, s[n] + 1);
        }
      }
      ans = min(ans, dp[i]);
    }
    printf("%d\n", ans);
    for (int i = 1; i <= n; i++) dp[i] = 0;
  }
  return 0;
}
