#include <bits/stdc++.h>
using namespace std;
int main() {
  int t;
  cin >> t;
  while (t--) {
    int n, k;
    cin >> n >> k;
    string s;
    cin >> s;
    int ans = 0, res = 0;
    for (int i = 0; i < n; i++)
      if (s[i] == '1') ans++;
    res = ans;
    for (int i = 0; i < k; i++) {
      int d = 0;
      for (int j = i; j < n; j = j + k) {
        if (s[j] == '1') {
          d++;
        } else
          d--;
        d = max(d, 0);
        res = min(res, ans - d);
      }
    }
    cout << res << endl;
  }
}
