#include <bits/stdc++.h>
using namespace std;
inline int64_t max(int64_t a, int64_t b) { return (a > b) ? a : b; }
inline int64_t min(int64_t a, int64_t b) { return (a < b) ? a : b; }
inline int64_t max3(int64_t a, int64_t b, int64_t c) {
  return max(a, max(b, c));
}
inline int64_t min3(int64_t a, int64_t b, int64_t c) {
  return min(a, min(b, c));
}
int64_t gcd(int64_t a, int64_t b) { return (a % b == 0) ? b : gcd(b, a % b); }
int64_t pow3(int64_t a, int64_t b, int64_t m = 1000000007) {
  int64_t res = 1;
  while (b > 0) {
    if (b & 1) res = (res * a) % m;
    a = (a * a) % m;
    b >>= 1;
  }
  return res;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int t = 1;
  cin >> t;
  while (t--) {
    string tt[3] = {"RGB", "GBR", "BRG"};
    int n, k;
    cin >> n >> k;
    string s;
    cin >> s;
    int ans = k - 1;
    for (int r = 0; r < 3; r++) {
      int dp[n];
      for (int i = 0; i < k - 1; i++) dp[i] = k + 1;
      for (int i = k - 1; i < n; i++) dp[i] = k;
      vector<char> check;
      for (int i = 0; i < k; i++) check.push_back(tt[r][i % 3]);
      int cnt = 0;
      for (int i = 0; i < k; i++)
        if (s[i] != check[i]) cnt++;
      dp[k - 1] = min(dp[k - 1], cnt);
      for (int i = k; i < n; i++) {
        dp[i] = dp[i - 1];
        if (*check.begin() == s[i - k]) dp[i]++;
        check.erase(check.begin());
        check.push_back(tt[r][i % 3]);
        if (*check.rbegin() == s[i]) dp[i]--;
      }
      for (int i = 0; i < n; i++) ans = min(ans, dp[i]);
    }
    cout << ans << endl;
  }
  return 0;
}
