#include <bits/stdc++.h>
using namespace std;
const int MaxN = 1000005;
int test, n, k;
int a[MaxN], ps[MaxN], f[MaxN];
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> test;
  while (test--) {
    cin >> n >> k;
    char ch;
    int Count = 0;
    for (int i = 1; i <= n; i++) {
      cin >> ch;
      a[i] = ch - '0';
      ps[i] = ps[i - 1] + a[i];
      f[i] = 0;
      Count += a[i];
    }
    if (Count == 0) {
      cout << 0 << '\n';
      continue;
    }
    for (int i = 1; i <= n; i++) {
      f[i] = min((i - k > 0 ? f[i - k] : 0) + ps[i - 1] - ps[max(0, i - k)],
                 ps[i - 1]) +
             1 - a[i];
    }
    int Ans = ps[n];
    for (int i = 1; i <= n; i++) {
      Ans = min(Ans, f[i] + ps[n] - ps[i]);
    }
    cout << Ans << '\n';
  }
  return 0;
}
