#include <bits/stdc++.h>
using namespace std;
int minmoves(string t) {
  int n = t.size();
  int pre[n];
  pre[0] = t[0] == '1' ? 1 : 0;
  for (int i = 1; i < n; i++) {
    if (t[i] == '0')
      pre[i] = pre[i - 1];
    else
      pre[i] = pre[i - 1] + 1;
  }
  int DP[n];
  int ans = pre[n - 1];
  for (int i = n - 1; i >= 0; i--) {
    if (i == n - 1) {
      DP[i] = t[i] == '1' ? 0 : 1;
    } else {
      DP[i] =
          min(DP[i + 1] + (t[i] == '0'), pre[n - 1] - pre[i] + (t[i] == '0'));
    }
    if (i == 0)
      ans = min(ans, DP[i]);
    else
      ans = min(ans, DP[i] + pre[i - 1]);
  }
  if (ans == INT_MAX) return 0;
  return ans;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int test_n;
  cin >> test_n;
  while (test_n--) {
    int n, k;
    cin >> n >> k;
    string s;
    cin >> s;
    int total1 = 0;
    for (int i = 0; i < n; i++) {
      if (s[i] == '1') total1 += 1;
    }
    int min_ = INT_MAX;
    for (int i = 0; i < k; i++) {
      if (i >= n) break;
      string t = "";
      for (int j = i; j < n; j += k) {
        t += s[j];
      }
      int this1 = 0;
      for (int j = 0; j < t.size(); j++) {
        if (t[j] == '1') this1 += 1;
      }
      min_ = min(minmoves(t) + total1 - this1, min_);
    }
    cout << min_ << '\n';
  }
}
