#include <bits/stdc++.h>
using namespace std;
long long mod = 1e9 + 7;
long long power(long long num, long long r) {
  if (r == 0) return 1;
  if (r == 1) return num % mod;
  long long ans = power(num, r / 2) % mod;
  if (r % 2 == 0) {
    return (ans * ans) % mod;
  }
  return (((ans * ans) % mod) * num) % mod;
}
long long modinv(long long num) { return power(num, mod - 2); }
int findset(int arr[], int x) {
  if (x != arr[x]) {
    arr[x] = findset(arr, arr[x]);
  }
  return arr[x];
}
void uni(int arr[], int ranks[], int a, int b) {
  int ra = findset(arr, a);
  int rb = findset(arr, b);
  if (ra != rb) {
    if (ranks[ra] < ranks[rb]) swap(ra, rb);
    arr[rb] = ra;
    if (ranks[ra] == ranks[rb]) {
      ranks[ra]++;
    }
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int t;
  cin >> t;
  while (t--) {
    int n, k;
    string s;
    cin >> n >> k >> s;
    int tot = 0;
    for (int i = 0; i < n; i++) {
      tot += s[i] - '0';
    }
    int ans = n;
    for (int i = 0; i < k; i++) {
      int count = 0;
      int dp[3] = {0}, tmp[3] = {0};
      for (int j = i; j < n; j += k) {
        for (int x = 0; x < 3; x++) tmp[x] = dp[x];
        if (s[j] == '0') {
          dp[0] = tmp[0];
          dp[1] = min(tmp[2], tmp[1]);
          dp[2] = min(tmp[2], tmp[0]) + 1;
        } else {
          count++;
          dp[0] = tmp[0] + 1;
          dp[1] = min(tmp[2], tmp[1]) + 1;
          dp[2] = min(tmp[2], tmp[0]);
        }
      }
      ans = min(ans, tot - count + min(dp[0], min(dp[1], dp[2])));
    }
    cout << ans << "\n";
  }
  return 0;
}
