#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
  int q;
  cin >> q;
  string R = "";
  string G = "";
  string B = "";
  for (int i = 0; i < 100000; ++i) {
    R += "RGB";
    G += "GBR";
    B += "BRG";
  }
  while (q--) {
    int N, K;
    cin >> N >> K;
    string S;
    cin >> S;
    vector<int> cnt(3, 0);
    vector<vector<int>> acnt(N + 1, vector<int>(3));
    int mx = 0;
    for (int i = 0; i < N; ++i) {
      if (S[i] == R[i])
        cnt[0]++;
      else
        cnt[0] = 0;
      if (S[i] == G[i])
        cnt[1]++;
      else
        cnt[1] = 0;
      if (S[i] == B[i])
        cnt[2]++;
      else
        cnt[2] = 0;
      mx = max(mx, cnt[0]);
      mx = max(mx, cnt[1]);
      mx = max(mx, cnt[2]);
      acnt[i + 1][0] = acnt[i][0] + (S[i] == R[i]);
      acnt[i + 1][1] = acnt[i][1] + (S[i] == G[i]);
      acnt[i + 1][2] = acnt[i][2] + (S[i] == B[i]);
    }
    int ans = max(K - mx, 0);
    for (int i = 0; i < N + 1; ++i) {
      for (int j = 0; j < 3; ++j)
        if (i - K >= 0) {
          int n = acnt[i][j] - acnt[i - K][j];
          ans = min(ans, K - n);
        }
    }
    cout << ans << '\n';
  }
  return 0;
}
