#include <bits/stdc++.h>
using namespace std;
const long long inf = 1e17;
long long fast_pow(long long x, long long y, long long p);
void solve() {
  long long n, k;
  cin >> n >> k;
  string s;
  cin >> s;
  long long ans = n;
  long long total = count((s).begin(), (s).end(), '1');
  for (long long i = 0; i < k; i++) {
    long long used = 0;
    for (long long j = i; j < n; j += k) {
      if (s[j] == '1')
        used++;
      else
        used--;
      if (used < 0) used = 0;
      ans = min(ans, total - used);
    }
  }
  cout << ans << "\n";
}
int32_t main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  long long test = 1;
  cin >> test;
  for (long long i = 1; i <= test; i++) {
    solve();
  }
  return 0;
}
long long fast_pow(long long x, long long y, long long p) {
  long long res = 1;
  x = x % p;
  while (y > 0) {
    if (y & 1) res = (res * x) % p;
    y = y >> 1;
    x = (x * x) % p;
  }
  return res;
}
