#include <bits/stdc++.h>
using namespace std;
int main() {
  int t;
  cin >> t;
  while (t--) {
    int n, k;
    cin >> n >> k;
    string s;
    cin >> s;
    int tot = 0;
    for (char ch : s)
      if (ch == '1') tot++;
    int ans = tot;
    for (int st = 0; st < k; st++) {
      int cost = 0, sum = 0;
      for (int i = st; i < n; i += k) {
        sum = max(sum, 0) + (s[i] == '1' ? 1 : -1);
        cost = max(cost, sum);
      }
      ans = min(ans, tot - cost);
    }
    cout << ans << '\n';
  }
  return 0;
}
