#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  long long t, i;
  cin >> t;
  while (t--) {
    long long n, k;
    cin >> n >> k;
    string s;
    cin >> s;
    long long one = 0;
    long long ans = INT_MAX;
    for (long long i = 0; i < n; i++)
      if (s[i] == '1') one++;
    for (long long i = 0; i < k; i++) {
      long long val = 0;
      for (long long j = i; j < n; j += k) {
        if (s[j] == '1')
          val++;
        else
          val--;
        if (val < 0) val = 0;
        ans = min(ans, one - val);
      }
    }
    cout << ans << "\n";
  }
  return 0;
}
