#include <bits/stdc++.h>
using namespace std;
int main(int argc, char const *argv[]) {
  int Q;
  std::cin >> Q;
  for (int q = 0; q < Q; q++) {
    int N, K;
    std::cin >> N >> K;
    string s;
    std::cin >> s;
    std::vector<std::vector<int> > dp =
        vector<vector<int> >(N, vector<int>(3, 0));
    string s1 = "RGB", s2 = "GBR", s3 = "BRG";
    int ans = 1000000007;
    for (int i = 0; i < K; i++) {
      if (s[i] != s1[i % 3]) dp[K - 1][0]++;
      if (s[i] != s2[i % 3]) dp[K - 1][1]++;
      if (s[i] != s3[i % 3]) dp[K - 1][2]++;
    }
    ans = min({ans, dp[K - 1][0], dp[K - 1][1], dp[K - 1][2]});
    for (int i = K; i < N; i++) {
      dp[i][0] = dp[i - 1][2];
      dp[i][1] = dp[i - 1][0];
      dp[i][2] = dp[i - 1][1];
      if (s[i] != s1[(K - 1) % 3]) dp[i][0]++;
      if (s[i] != s2[(K - 1) % 3]) dp[i][1]++;
      if (s[i] != s3[(K - 1) % 3]) dp[i][2]++;
      if (s[i - K] != s3[0]) dp[i][0]--;
      if (s[i - K] != s1[0]) dp[i][1]--;
      if (s[i - K] != s2[0]) dp[i][2]--;
      ans = min({ans, dp[i][0], dp[i][1], dp[i][2]});
    }
    std::cout << ans << '\n';
  }
  return 0;
}
