#include <bits/stdc++.h>
using namespace std;
const long long mod = 1e9 + 7;
template <typename Arg1>
void __f(const char* name, Arg1&& arg1) {
  cerr << name << " = " << arg1 << '\n';
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args) {
  const char* comma = strchr(names + 1, ',');
  cerr.write(names, comma - names) << " = " << arg1 << " || ";
  __f(comma + 1, args...);
}
template <class Ch, class Tr, class Container>
basic_ostream<Ch, Tr>& operator<<(basic_ostream<Ch, Tr>& os,
                                  Container const& x) {
  os << "{ ";
  for (auto& y : x) os << y << " ; ";
  return os << "}";
}
template <class X, class Y>
ostream& operator<<(ostream& os, pair<X, Y> const& p) {
  return os << "[ " << p.first << ", " << p.second << "]";
}
long long power(long long x, long long y, long long p) {
  long long res = 1;
  x = x % p;
  while (y > 0) {
    if (y & 1) res = (res * x) % p;
    y = y >> 1;
    x = (x * x) % p;
  }
  return res;
}
long long maxsubarrsum(vector<long long> vec) {
  vector<long long> pref((long long)(vec.size()), 0);
  long long ans1 = 0;
  for (long long i = 0; i < (long long)(vec.size()); i++) {
    if (vec[i] > 0) ans1 += vec[i];
    if (i == 0)
      pref[i] = vec[i];
    else {
      pref[i] = pref[i - 1] + vec[i];
    }
  }
  long long ans2 = 0;
  long long last = 0;
  for (long long i = 0; i < (long long)(vec.size()); i++) {
    ans2 = max(ans2, pref[i] - last);
    last = min(last, pref[i]);
  }
  return ans1 - ans2;
}
int main() {
  ios_base::sync_with_stdio(false);
  long long t;
  cin >> t;
  while (t--) {
    long long n, k;
    cin >> n >> k;
    string s;
    cin >> s;
    long long tot = 0;
    for (long long i = 0; i < (long long)(s.size()); i++) {
      if (s[i] == '1') tot++;
    }
    long long ans = 1e18;
    for (long long i = 0; i < k; i++) {
      vector<long long> temp;
      long long temp1 = 0;
      long long f = 0;
      long long c = 0;
      for (long long j = i; j < n + k; j += k) {
        if (j < n && f == 0 && s[j] == '0') {
          c++;
        } else if (j < n && f == 1 && s[j] == '1') {
          c++;
        } else {
          if (c > 0) {
            if (f == 0) {
              if ((long long)(temp.size()) > 0) {
                temp.push_back(-c);
              }
            } else {
              temp.push_back(c);
            }
          }
          c = 1;
          f = 1 - f;
        }
      }
      long long c1 = 0;
      for (long long j = 0; j < (long long)(temp.size()); j++) {
        if (temp[j] > 0) c1 += temp[j];
      }
      if ((long long)(temp.size()) > 0 &&
          temp[(long long)(temp.size()) - 1] < 0)
        temp.pop_back();
      long long temp2 = maxsubarrsum(temp);
      ans = min(ans, temp2 + tot - c1);
    }
    cout << ans << '\n';
  }
  return 0;
}
