#include <bits/stdc++.h>
using namespace std;
void enter(vector<long long int> &ar) {
  long long int n = ar.size();
  for (long long int i = 0; i < n; i++) {
    cin >> ar[i];
  }
}
void show(vector<long long int> &a) {
  long long int n = a.size();
  for (long long int i = 0; i < n; i++) {
    cout << a[i] << " ";
  }
  cout << "\n";
}
void SieveOfEratosthenes(int n, vector<long long int> &pri) {
  vector<bool> prime(n + 1, true);
  for (int p = 2; p * p <= n; p++) {
    if (prime[p] == true) {
      pri.push_back(p);
      for (int i = p * p; i <= n; i += p) prime[i] = false;
    }
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  long long int t;
  cin >> t;
  while (t--) {
    long long int n, k;
    cin >> n >> k;
    string s;
    cin >> ws;
    cin >> s;
    cin >> ws;
    vector<long long int> a(n);
    for (long long int i = 0; i < n; i++) {
      a[i] = s[i] - '0';
    }
    vector<long long int> sum(n, 0);
    sum[0] = a[0];
    for (long long int i = 1; i < n; i++) {
      sum[i] = sum[i - 1] + a[i];
    }
    vector<long long int> dp(n);
    for (long long int i = n - 1; i >= 0; i--) {
      long long int add_one = a[i];
      if (i + k <= n - 1) {
        dp[i] = min(dp[i + k] + ((a[i] == 1) ? 0 : 1) + sum[i + k - 1] - sum[i],
                    ((a[i] == 1) ? 1 : 0) + sum[n - 1] - sum[i]);
      } else {
        dp[i] = min(((a[i] == 1) ? 0 : 1) + sum[n - 1] - sum[i],
                    ((a[i] == 1) ? 1 : 0) + sum[n - 1] - sum[i]);
      }
    }
    long long int ans;
    for (long long int i = 0; i < n; i++) {
      if (i) {
        dp[i] += sum[i - 1];
        ans = min(ans, dp[i]);
      } else {
        ans = dp[i];
      }
    }
    cout << ans << "\n";
  }
  return 0;
}
