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