#include <bits/stdc++.h>
using namespace std;
const long long INF = 1e18;
const long long MAX = 1e5 + 10;
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int t = 1;
  cin >> t;
  while (t--) {
    int n, k;
    cin >> n >> k;
    string s;
    cin >> s;
    vector<int> p(n + 1, 0);
    vector<vector<int>> dp(n + 1, vector<int>(2, 0));
    for (int i = 1; i < n + 1; i++) p[i] = p[i - 1] + s[i - 1] - '0';
    for (int i = 1; i < k + 1; i++) {
      dp[i][0] = min(dp[i - 1][0], dp[i - 1][1]) + s[i - 1] - '0';
      dp[i][1] = p[i - 1] + '1' - s[i - 1];
    }
    for (int i = k + 1; i < n + 1; i++) {
      if (s[i - 1] == '0')
        dp[i][0] = min(dp[i - 1][0], dp[i - 1][1]),
        dp[i][1] = min(p[i - 1] + 1, dp[i - k][1] + 1 + p[i - 1] - p[i - k]);
      else
        dp[i][0] = min(dp[i - 1][0], dp[i - 1][1]) + 1,
        dp[i][1] = min(p[i - 1], dp[i - k][1] + p[i - 1] - p[i - k]);
    }
    cout << min(dp[n][0], dp[n][1]) << "\n";
  }
}
