#include <bits/stdc++.h>
using namespace std;
long long int min(long long int a, long long int b) { return a < b ? a : b; }
long long int min(long long int a, long long int b, long long int c) {
  a = min(a, b);
  a = min(a, c);
  return a;
}
long long int max(long long int a, long long int b) { return a > b ? a : b; }
long long int max(long long int a, long long int b, long long int c) {
  a = max(a, b);
  a = max(a, c);
  return a;
}
long long int dsum(long long int n) {
  long long int ans = 0;
  while (n) {
    ans += (n % 10);
    n /= 10;
  }
  return ans;
}
long long int power(long long int a, long long int b) {
  long long int res = 1;
  while (b) {
    if (b & 1) res = res * a;
    a = a * a;
    b >>= 1;
  }
  return res;
}
template <typename T>
istream &operator>>(istream &is, vector<T> &v) {
  for (T &t : v) is >> t;
  return is;
}
template <typename T>
ostream &operator<<(ostream &os, const vector<T> &v) {
  for (const T &t : v) {
    os << t << " ";
  }
  os << '\n';
  return os;
}
struct pll {
  long long int x, y;
};
bool comp(pll a, pll b) {
  if (a.x == b.x)
    return a.y < b.y;
  else
    return a.x < b.x;
}
void solve(long long int the) {
  char ch;
  double aa, bb, cc, dd, xx;
  string s1, s2, s3, str;
  long long int i, j, k, a, b, c, d, n, m, l, r, x, y, z, low, high, mid,
      sum = 0, ans = 0, temp, t;
  cin >> n >> k;
  cin >> str;
  str = " " + str;
  vector<long long int> fr(n + 1, 0);
  fr[0] = 0;
  for (i = 1; i < n + 1; i++) {
    fr[i] = fr[i - 1];
    if (str[i] == '1') fr[i]++;
  }
  long long int dp[n + 1][2];
  dp[0][0] = 0;
  dp[0][1] = 0;
  for (i = 1; i < n + 1; i++) {
    dp[i][0] = 1e18;
    dp[i][1] = 1e18;
  }
  for (i = 1; i < n + 1; i++) {
    if (str[i] == '0') {
      dp[i][0] = min(dp[i - 1][0], dp[i - 1][1]);
      dp[i][1] = fr[i - 1] + 1;
      a = 1e18;
      if (i - k > 0) {
        if (str[i - k] == '0')
          a = dp[i - k][1] + fr[i - 1] - fr[i - k] + 1;
        else
          a = dp[i - k][0] + fr[i - 1] - fr[i - k] + 1;
      }
      dp[i][1] = min(a, dp[i][1]);
    } else {
      dp[i][1] = 1 + min(dp[i - 1][0], dp[i - 1][1]);
      dp[i][0] = fr[i - 1];
      a = 1e18;
      if (i - k > 0) {
        if (str[i - k] == '0')
          a = dp[i - k][1] + fr[i - 1] - fr[i - k];
        else
          a = dp[i - k][0] + fr[i - 1] - fr[i - k];
      }
      dp[i][0] = min(a, dp[i][0]);
    }
  }
  ans = min(dp[n][0], dp[n][1]);
  cout << ans << endl;
}
int main() {
  long long int t = 1, c = 1;
  scanf("%lld", &t);
  while (t--) {
    solve(c);
    c++;
  }
  return 0;
}
