#include <bits/stdc++.h>
using namespace std;
template <typename T>
inline void read(T& x) {
  int f = 0, c = getchar();
  x = 0;
  while (!isdigit(c)) f |= c == '-', c = getchar();
  while (isdigit(c)) x = x * 10 + c - 48, c = getchar();
  if (f) x = -x;
}
template <typename T, typename... Args>
inline void read(T& x, Args&... args) {
  read(x);
  read(args...);
}
template <typename T>
void write(T x) {
  if (x < 0) x = -x, putchar('-');
  if (x > 9) write(x / 10);
  putchar(x % 10 + 48);
}
template <typename T>
void writeln(T x) {
  write(x);
  puts("");
}
template <typename T>
inline bool chkmin(T& x, const T& y) {
  return y < x ? (x = y, true) : false;
}
template <typename T>
inline bool chkmax(T& x, const T& y) {
  return x < y ? (x = y, true) : false;
}
const int maxn = (int)2e5 + 10;
const int inf = (int)1e9;
const int mod = (int)1e9 + 7;
int main() {
  int q, n, k;
  read(q);
  string s;
  while (q--) {
    read(n, k);
    cin >> s;
    int ans = inf;
    string s1 = "RGB", s2 = "BRG", s3 = "GBR";
    string t1, t2, t3;
    for (int i = (0); i < (n); i++) t1.push_back(s1[i % 3]);
    for (int i = (0); i < (n); i++) t2.push_back(s2[i % 3]);
    for (int i = (0); i < (n); i++) t3.push_back(s3[i % 3]);
    int sum = 0;
    for (int i = (0); i < (k); i++) {
      if (t1[i] != s[i]) sum++;
    }
    chkmin(ans, sum);
    for (int i = k, j = 0; i < n; i++, j++) {
      if (s[j] != t1[j]) sum--;
      if (s[i] != t1[i]) sum++;
      chkmin(ans, sum);
    }
    sum = 0;
    for (int i = (0); i < (k); i++) {
      if (t2[i] != s[i]) sum++;
    }
    chkmin(ans, sum);
    for (int i = k, j = 0; i < n; i++, j++) {
      if (s[j] != t2[j]) sum--;
      if (s[i] != t2[i]) sum++;
      chkmin(ans, sum);
    }
    sum = 0;
    for (int i = (0); i < (k); i++) {
      if (t3[i] != s[i]) sum++;
    }
    chkmin(ans, sum);
    for (int i = k, j = 0; i < n; i++, j++) {
      if (s[j] != t3[j]) sum--;
      if (s[i] != t3[i]) sum++;
      chkmin(ans, sum);
    }
    cout << ans << endl;
  }
}
