#include <bits/stdc++.h>
using namespace std;
signed main() {
  long long t;
  cin >> t;
  while (t--) {
    long long n, k;
    cin >> n >> k;
    string s;
    cin >> s;
    long long pref[n + 1];
    pref[0] = 0;
    for (long long i = 1; i <= n; i++) {
      pref[i] = (s[i - 1] - '0');
      pref[i] += pref[i - 1];
    }
    long long dp[n];
    for (long long i = 1; i <= n; i++) {
      dp[i] = 10000000000000000;
      dp[i] = pref[i - 1];
      if ((i - k) >= 1) {
        long long cost = dp[i - k] + pref[i - 1] - pref[i - k];
        dp[i] = min(dp[i], cost);
      }
      if (s[i - 1] == '0') dp[i]++;
    }
    long long ans = pref[n];
    for (long long i = 1; i <= n; i++) {
      ans = min(ans, dp[i] + pref[n] - pref[i]);
    }
    cout << ans << endl;
  }
}
