#include <bits/stdc++.h>
using namespace std;
mt19937 rnd(chrono::high_resolution_clock::now().time_since_epoch().count());
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  int t;
  cin >> t;
  while (t--) {
    int n, k;
    cin >> n >> k;
    string s;
    cin >> s;
    int o = 0;
    for (int i = 0; i < n; i++) {
      if (s[i] == '1') o++;
    }
    long long ans = 1e18;
    for (int mod = 0; mod < k; mod++) {
      long long delta = 0;
      for (int i = mod; i < n; i += k) {
        if (s[i] == '0')
          delta++;
        else
          delta--;
        delta = min(delta, 0ll);
        ans = min(ans, o + delta);
      }
    }
    cout << ans << '\n';
  }
}
