#include <bits/stdc++.h>
using namespace std;
template <class T>
ostream& operator<<(ostream& os, vector<T> V) {
  os << "[ ";
  for (auto v : V) os << v << " ";
  return os << "]";
}
template <class L, class R>
ostream& operator<<(ostream& os, pair<L, R> P) {
  return os << "(" << P.first << "," << P.second << ")";
}
template <typename Arg1>
void __f(const char* name, Arg1&& arg1) {
  cout << name << " : " << arg1 << std::endl;
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args) {
  const char* comma = strchr(names + 1, ',');
  cout.write(names, comma - names) << " : " << arg1 << " | ";
  __f(comma + 1, args...);
}
const long long Mod = 1e6 + 7;
int A[3][3];
string match = "RGB";
int ans;
void relax() {
  ans = min(ans, A[0][1] + A[0][2] + A[1][0] + A[1][2] + A[2][0] + A[2][1]);
  ans = min(ans, A[0][0] + A[0][2] + A[1][0] + A[1][1] + A[2][1] + A[2][2]);
  ans = min(ans, A[0][0] + A[0][1] + A[1][1] + A[1][2] + A[2][0] + A[2][2]);
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int q;
  cin >> q;
  while (q--) {
    ans = Mod;
    memset(A, 0, sizeof(A));
    int n, k;
    cin >> n >> k;
    string s;
    cin >> s;
    for (int i = 0; i < k; i++) {
      for (int j = 0; j < 3; j++) {
        if (s[i] == match[j]) A[i % 3][j]++;
      }
    }
    relax();
    for (int i = k; i < n; i++) {
      for (int j = 0; j < 3; j++) {
        if (s[i - k] == match[j]) A[0][j]--;
        if (s[i] == match[j]) A[k % 3][j]++;
      }
      int B[3][3];
      memset(B, 0, sizeof(B));
      for (int j = 0; j < 3; j++) {
        for (int z = 0; z < 3; z++) {
          B[j][z] = A[(j + 1) % 3][z];
        }
      }
      for (int j = 0; j < 3; j++) {
        for (int z = 0; z < 3; z++) {
          A[j][z] = B[j][z];
        }
      }
      relax();
    }
    cout << ans << '\n';
  }
  return 0;
}
