#include <bits/stdc++.h>
using namespace std;
const long long MOD = 998244353;
long long power(long long n, long long p) {
  if (p == 0) return 1;
  if (p == 1) return n;
  if (p % 2)
    return power(n, p - 1) * n;
  else {
    long long x = power(n, p / 2);
    return x * x;
  }
}
long long fact(long long num) {
  long long fac = 1;
  while (num != 1) {
    fac = fac * num;
    num--;
  }
  return fac;
}
const int N = 2e5 + 5;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  long long t = 1;
  cin >> t;
  while (t--) {
    long long n, k, cnt = 0, i, j;
    string s;
    cin >> n >> k;
    cin >> s;
    for (i = 0; i < n; i++)
      if (s[i] == '1') cnt++;
    long long ans = cnt;
    for (i = 0; i < k; i++) {
      long long x = 0, y = 0;
      for (j = i; j < n; j += k) {
        if (s[j] == '1')
          x++;
        else
          y++;
        if (x > y)
          ans = min(ans, cnt - x + y);
        else
          x = 0, y = 0;
      }
    }
    cout << ans << endl;
  }
  return 0;
}
