#include <bits/stdc++.h>
using namespace std;
struct custom_hash {
  static uint64_t splitmix64(uint64_t x) {
    x += 0x9e3779b97f4a7c15;
    x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
    x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
    return x ^ (x >> 31);
  }
  size_t operator()(uint64_t x) const {
    static const uint64_t FIXED_RANDOM =
        chrono::steady_clock::now().time_since_epoch().count();
    return splitmix64(x + FIXED_RANDOM);
  }
};
const int N = 1e6 + 1;
const int mod = 998244353;
long long int a[N], b[N], c[N];
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int t;
  cin >> t;
  while (t--) {
    int n, k;
    string s;
    cin >> n >> k >> s;
    for (int i = 1; i <= n; ++i) {
      a[i] = a[i - 1] + s[i - 1] - '0';
    }
    long long int ans = a[n];
    for (int i = 1; i <= n; ++i) {
      b[i] = s[i - 1] == '0';
      long long int lr =
          a[i - 1] - (i > k ? a[i - k] : 0) + (i > k ? b[i - k] : 0);
      b[i] += min(lr, a[i - 1]);
      ans = min(ans, b[i] + a[n] - a[i]);
    }
    cout << ans << endl;
  }
}
