#include <bits/stdc++.h>
#pragma GCC target("avx2")
#pragma GCC optimization("O3")
#pragma GCC optimization("unroll-loops")
const long long INF = 0x3f3f3f3f3f3f3f3f;
const long long llinf = (1LL << 62);
const int inf = (1 << 30);
const int nmax = 2e5 + 50;
const long long mod = 1e9 + 7;
using namespace std;
int q, i, n, k, x, y, z, j, t, s, a[nmax], nr[nmax][3][3], mn, l;
char c;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cerr.tie(0);
  cout.tie(0);
  cin >> q;
  while (q--) {
    cin >> n >> k;
    for (i = 1; i <= n; i++) {
      cin >> c;
      if (c == 'R') a[i] = 0;
      if (c == 'G') a[i] = 1;
      if (c == 'B') a[i] = 2;
      for (j = 0; j < 3; j++)
        for (t = 0; t < 3; t++) nr[i][j][t] = 0;
    }
    for (i = 1; i <= n; i++) {
      for (j = 0; j < 3; j++)
        for (t = 0; t < 3; t++) nr[i][j][t] = nr[i - 1][j][t];
      nr[i][i % 3][a[i]]++;
    }
    mn = 1e9;
    for (i = k; i <= n; i++) {
      l = i - k;
      for (x = 0; x < 3; x++) {
        s = 0;
        for (y = 0; y < 3; y++) {
          z = (x + y) % 3;
          j = (l + y + 1) % 3;
          s += nr[i][j][z] - nr[l][j][z];
        }
        mn = min(mn, k - s);
      }
    }
    cout << mn << '\n';
  }
  return 0;
}
