#include <bits/stdc++.h>
using namespace std;
const int mod = 1000000007;
struct node {
  long long len, l, r;
  bool inline operator<(const node &a) const {
    return this->len == a.len ? this->l < a.l : this->len > a.len;
  }
};
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  long long t, n, m, k, q, x, a, b, c, d, y;
  cin >> t;
  while (t--) {
    cin >> n >> k;
    vector<int> v(n), vv(n);
    string s;
    cin >> s;
    for (int i = 0; i < n; i++) {
      if (s[i] == '1') {
        v[i] = 1;
        vv[i] = 1;
      } else {
        v[i] = 0;
        vv[i] = 0;
      }
    }
    reverse((vv).begin(), (vv).end());
    vector<int> f(n), r(n);
    for (int i = 1; i < n; i++) {
      v[i] += v[i - 1];
      vv[i] += vv[i - 1];
    }
    for (int i = 0; i < n; i++) {
      if (s[i] == '1') {
        if (i < k) {
          f[i] = v[i] - 1;
        } else {
          f[i] = min(v[i] - v[i - k] - 1 + f[i - k], v[i] - 1);
        }
      } else {
        if (i < k) {
          f[i] = v[i] + 1;
        } else {
          f[i] = min(v[i] - v[i - k] + 1 + f[i - k], v[i] + 1);
        }
      }
      if (s[n - 1 - i] == '1') {
        if (i < k) {
          r[i] = vv[i] - 1;
        } else {
          r[i] = min(vv[i] - vv[i - k] - 1 + r[i - k], vv[i] - 1);
        }
      } else {
        if (i < k) {
          r[i] = vv[i] + 1;
        } else {
          r[i] = min(vv[i] - vv[i - k] + 1 + r[i - k], vv[i] + 1);
        }
      }
    }
    reverse((r).begin(), (r).end());
    a = 1e9;
    for (int i = 0; i < n; i++) {
      if (s[i] == '1') {
        a = min((int)a, r[i] + f[i]);
      } else {
        a = min((int)a, r[i] + f[i] - 1);
      }
    }
    a = min((int)a, v[n - 1]);
    cout << a << '\n';
  }
  return 0;
}
