#include <bits/stdc++.h>
using namespace std;
int dp[200000][3];
const char kavo[] = "RGB";
int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int q;
  cin >> q;
  while (q--) {
    int n, k;
    string s;
    cin >> n >> k >> s;
    for (int i = 0; i < n; ++i) dp[i][0] = dp[i][1] = dp[i][2] = 0;
    for (int j = 0; j < k; j++)
      for (int i = 0; i < 3; i++)
        dp[j][i] = (j != 0 ? dp[j - 1][(i + 2) % 3] : 0) + int(s[j] != kavo[i]);
    int ans = min(dp[k - 1][0], min(dp[k - 1][1], dp[k - 1][2]));
    for (int j = k; j < n; j++) {
      for (int i = 0; i < 3; i++) {
        dp[j][i] = dp[j - 1][(i + 2) % 3] + int(s[j] != kavo[i]);
      }
    }
    for (int j = k; j < n; j++)
      for (int i = 0; i < 3; i++)
        ans = min(ans, dp[j][i] - dp[j - k][(3 + i - (k % 3)) % 3]);
    cout << ans << "\n";
  }
  return 0;
}
