#include <bits/stdc++.h>
using namespace std;
void __print(int x) { cerr << x; }
void __print(long x) { cerr << x; }
void __print(long long x) { cerr << x; }
void __print(unsigned x) { cerr << x; }
void __print(unsigned long x) { cerr << x; }
void __print(unsigned long long x) { cerr << x; }
void __print(float x) { cerr << x; }
void __print(double x) { cerr << x; }
void __print(long double x) { cerr << x; }
void __print(char x) { cerr << '\'' << x << '\''; }
void __print(const char *x) { cerr << '\"' << x << '\"'; }
void __print(const string &x) { cerr << '\"' << x << '\"'; }
void __print(bool x) { cerr << (x ? "true" : "false"); }
template <typename T, typename V>
void __print(const pair<T, V> &x) {
  cerr << '{';
  __print(x.first);
  cerr << ',';
  __print(x.second);
  cerr << '}';
}
template <typename T>
void __print(const T &x) {
  int f = 0;
  cerr << '{';
  for (auto &i : x) cerr << (f++ ? "," : ""), __print(i);
  cerr << "}";
}
void _print() { cerr << "]\n"; }
template <typename T, typename... V>
void _print(T t, V... v) {
  __print(t);
  if (sizeof...(v)) cerr << ", ";
  _print(v...);
}
clock_t time_p = clock();
void rtime() {
  time_p = clock() - time_p;
  cerr << "Time Taken : " << (float)1000 * (time_p) / CLOCKS_PER_SEC << "\n";
}
void solve() {
  long long n, k;
  cin >> n >> k;
  string str;
  cin >> str;
  vector<long long> psum(n);
  for (long long i = 0; i < n; i++) {
    psum[i] = str[i] - '0';
    psum[i] += (i - 1 >= 0) ? psum[i - 1] : 0;
  }
  vector<long long> dp1(n), dp2(n);
  if (str[0] == '1') {
    dp1[0] = 0;
    dp2[0] = 1;
  } else {
    dp2[0] = 0;
    dp1[0] = 1;
  }
  for (long long i = 1; i < n; i++) {
    long long val1 = min(dp1[i - 1], dp2[i - 1]);
    long long val2 = min(((i - k >= 0) ? dp1[i - k] : 0) +
                             (psum[i - 1] - ((i - k >= 0) ? psum[i - k] : 0)),
                         psum[i - 1]);
    if (str[i] == '1') {
      dp1[i] = val2;
      dp2[i] = 1 + val1;
    } else {
      dp2[i] = val1;
      dp1[i] = 1 + val2;
    }
  }
  cout << min(dp1[n - 1], dp2[n - 1]) << "\n";
}
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  long long t = 1;
  cin >> t;
  while (t--) {
    solve();
  }
  return 0;
}
