#include <bits/stdc++.h>
using namespace std;
int f(string s, int x) {
  int len = s.size();
  int ans = x;
  int res[len];
  int pd = 0;
  for (int i = 0; i < len; i++) {
    int f = 0;
    if (s.at(i) == '1') {
      pd++;
      res[i] = 0;
      f = 1;
    } else {
      res[i] = 1;
    }
    if (i > 0) res[i] += min(res[i - 1], pd - f);
    ans = min(ans, res[i] + x - pd);
  }
  return ans;
}
void solve() {
  int n, k, total = 0;
  cin >> n >> k;
  string s;
  cin >> s;
  string p[k];
  for (int i = 0; i < n; i++) {
    p[i % k] += s.at(i);
    if (s.at(i) == '1') {
      total++;
    }
  }
  int ans = total;
  for (int i = 0; i < k; i++) {
    int x = 0;
    for (int j = 0; j < p[i].size(); j++) {
      if (p[i].at(j) == '1') {
        x++;
      }
    }
    ans = min(ans, f(p[i], x) + total - x);
  }
  cout << ans << "\n";
}
int main() {
  int t;
  scanf("%d", &t);
  for (int i = 0; i < t; i++) {
    solve();
  }
  return 0;
}
