#include <bits/stdc++.h>
#pragma GCC optimize("O3")
using namespace std;
const int MAX = 2e5 + 5;
const long long MAX2 = 11;
const long long MOD = 998244353;
const long long INF = 2e18;
const int dr[] = {1, 0, -1, 0, 1, 1, -1, -1, 0};
const int dc[] = {0, 1, 0, -1, 1, -1, 1, -1, 0};
const double pi = acos(-1);
const double EPS = 1e-9;
const int block = 450;
int tc, n, k, cnt[3][3], hs[128], tmp[3][3], z, ans, hit;
string s;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  hs['R'] = 0, hs['G'] = 1, hs['B'] = 2;
  cin >> tc;
  while (tc--) {
    cin >> n >> k >> s;
    memset(cnt, 0, sizeof cnt);
    n--, k--;
    for (long long i = 0; i <= k - 1; ++i) {
      z = hs[s[i]];
      for (long long j = 0; j <= 2; ++j)
        if ((i + j) % 3 == z) ++cnt[j][z];
    }
    ans = 1e9;
    for (long long i = k; i <= n; ++i) {
      z = hs[s[i]];
      for (long long j = 0; j <= 2; ++j)
        if ((k + j) % 3 == z) ++cnt[j][z];
      for (long long j = 0; j <= 2; ++j) {
        hit = 0;
        for (long long o = 0; o <= 2; ++o) hit += cnt[j][o];
        ans = min(ans, k - hit + 1);
      }
      z = hs[s[i - k]];
      for (long long j = 0; j <= 2; ++j)
        if (j % 3 == z) --cnt[j][z];
      for (long long j = 0; j <= 2; ++j)
        for (long long o = 0; o <= 2; ++o) tmp[(j + 1) % 3][o] = cnt[j][o];
      for (long long j = 0; j <= 2; ++j)
        for (long long o = 0; o <= 2; ++o) cnt[j][o] = tmp[j][o];
    }
    cout << ans << "\n";
  }
  return 0;
}
