#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int t;
  cin >> t;
  while (t--) {
    int n, k, a, b, res = 1 << 30, Max = 0, cur = 0, aux = 0;
    cin >> n >> k;
    string cad;
    cin >> cad;
    int tuno = 0;
    for (int i = 0; i < n; ++i) {
      if (cad[i] == '1') tuno++;
    }
    for (int i = 0; i < k; ++i) {
      aux = -1;
      a = -1;
      b = -1;
      Max = 0;
      cur = 0;
      for (int j = i; j < n; j += k) {
        if (cad[j] == '1') {
          cur++;
          if (aux == -1) {
            aux = j;
          }
          if (cur > Max) {
            Max = cur;
            b = j;
            a = aux;
          }
        } else {
          cur--;
        }
        if (cur < 0) {
          aux = -1;
          cur = 0;
        }
      }
      int zero = 0;
      int uno = 0;
      if (a != -1 && b != -1) {
        for (int j = a; j <= b; j += k) {
          if (cad[j] == '0')
            zero++;
          else
            uno++;
        }
        res = min(res, tuno - uno + zero);
      }
    }
    if (res == 1 << 30) res = 0;
    cout << res << endl;
  }
  return 0;
}
