#include <bits/stdc++.h>
using namespace std;
const long long Mod = 1e18;
long long poww(long long a, long long b, long long Mod) {
  long long res = 1;
  if (b < 0) b = (b % (Mod - 1) + Mod - 1) % (Mod - 1);
  for (; b; b >>= 1, a = 1ll * a * a % Mod)
    if (b & 1) res = 1ll * res * a % Mod;
  return res;
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  long long t;
  cin >> t;
  while (t--) {
    string s;
    long long n, k, aans = 1e9, B[4], ans = 1e14;
    cin >> n >> k >> s;
    char A[] = {'R', 'G', 'B'};
    long long dp[200200][4];
    if (s[0] != A[0]) dp[1][0] = 1;
    if (s[0] != A[1]) dp[1][1] = 1;
    if (s[0] != A[2]) dp[1][2] = 1;
    if (k % 3 == 0) {
      B[0] = 0;
      B[1] = 1;
      B[2] = 2;
    } else if (k % 3 == 1) {
      B[0] = 2;
      B[1] = 0;
      B[2] = 1;
    } else {
      B[0] = 1;
      B[1] = 2;
      B[2] = 0;
    }
    for (int i = 1; i <= k; i++) {
      if (s[i - 1] != A[0])
        dp[i][0] = dp[i - 1][2] + 1;
      else
        dp[i][0] = dp[i - 1][2];
      if (s[i - 1] != A[1])
        dp[i][1] = dp[i - 1][0] + 1;
      else
        dp[i][1] = dp[i - 1][0];
      if (s[i - 1] != A[2])
        dp[i][2] = dp[i - 1][1] + 1;
      else
        dp[i][2] = dp[i - 1][1];
    }
    for (int i = k + 1; i <= n; i++) {
      dp[i][0] = dp[i - 1][2] + (s[i - 1] != A[0]);
      dp[i][1] = dp[i - 1][0] + (s[i - 1] != A[1]);
      dp[i][2] = dp[i - 1][1] + (s[i - 1] != A[2]);
    }
    dp[0][0] = 0;
    dp[0][1] = 0;
    dp[0][2] = 0;
    for (int i = k; i <= n; i++) {
      ans = min(min(ans, dp[i][0] - dp[i - k][B[0]]),
                min(dp[i][1] - dp[i - k][B[1]], dp[i][2] - dp[i - k][B[2]]));
    }
    cout << ans << '\n';
  }
  return 0;
}
