#include <bits/stdc++.h>
using namespace std;
void R() {}
template <class T>
void _R(T &x) {
  cin >> x;
}
void _R(signed &x) { scanf("%d", &x); }
void _R(long long &x) { scanf("%I64d", &x); }
void _R(double &x) { scanf("%lf", &x); }
void _R(char &x) { scanf(" %c", &x); }
void _R(char *x) { scanf("%s", x); }
template <class T, class... Tx>
void R(T &head, Tx &...tail) {
  _R(head);
  R(tail...);
}
template <class T>
void _W(const T &x) {
  cout << x;
}
void _W(const signed &x) { printf("%d", x); }
void _W(const long long &x) { printf("%I64d", x); }
void _W(const double &x) { printf("%.16f", x); }
void _W(const char &x) { putchar(x); }
void _W(const char *x) { printf("%s", x); }
template <class T, class U>
void _W(const pair<T, U> &x) {
  _W(x.F);
  putchar(' ');
  _W(x.S);
}
template <class T>
void _W(const vector<T> &x) {
  for (auto i = x.begin(); i != x.end(); _W(*i++))
    if (i != x.cbegin()) putchar(' ');
}
void W() {}
template <class T, class... U>
void W(const T &head, const U &...tail) {
  _W(head);
  putchar(sizeof...(tail) ? ' ' : '\n');
  W(tail...);
}
const double EPS = 1e-7;
const long long N = 1e6 + 5;
long long n, k, f[N][3], a[N];
char s[N];
inline long long id(char x) {
  if (x == 'R') return 0;
  if (x == 'G') return 1;
  return 2;
}
signed main() {
  long long T;
  cin >> T;
  while (T--) {
    R(n, k);
    scanf("%s", s + 1);
    for (long long i = 1; i <= n; ++i) a[i] = id(s[i]);
    f[0][0] = f[0][1] = f[0][2] = 0;
    for (long long i = 1; i <= n; ++i) {
      for (long long j = 0; j <= 2; ++j) {
        f[i][j] = f[i - 1][j] + (a[i] != (j + i) % 3);
      }
    }
    long long ans = 2e18;
    for (long long i = k; i <= n; ++i) {
      for (long long j = 0; j <= 2; ++j) {
        ans = min(ans, f[i][j] - f[i - k][j]);
      }
    }
    W(ans);
  }
  return 0;
}
