#include <bits/stdc++.h>
using namespace std;
int T, N, K;
string s;
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cin >> T;
  while (T--) {
    cin >> N >> K >> s;
    s = '#' + s;
    int one = 0;
    for (int i = 1; i <= N; i++)
      if (s[i] == '1') one++;
    if (one <= 1) {
      cout << 0 << "\n";
      continue;
    }
    int res = one;
    for (int x = 1; x <= K; x++) {
      int suf0 = 0, suf1 = 0;
      for (int i = x; i <= N; i += K) {
        if (s[i] == '1')
          suf1++;
        else
          suf0++;
      }
      int out = one - suf1;
      int val = 1234567890, mn = suf0, pre0 = 0, pre1 = 0;
      for (int i = x; i <= N; i += K) {
        if (s[i] == '1')
          pre1++, suf1--;
        else
          pre0++, suf0--;
        mn = min(mn, pre1 + suf0);
        val = min(val, mn + suf1 - suf0);
      }
      val += out;
      res = min(res, val);
    }
    cout << res << "\n";
  }
  return 0;
}
