#include <bits/stdc++.h>
#pragma warning(disable : 4996)
#pragma comment(linker, "/STACK:102400000,102400000")
int ok, _ncas;
const int mod = 998244353;
const long long INF = 0x3f3f3f3f3f3f3f3f;
const int inf = 0x3f3f3f3f;
using namespace std;
const int maxn = 2e5 + 700;
char s1[maxn], s2[maxn], s3[maxn];
char s[maxn];
void init() {
  char s4[3];
  s4[0] = 'R';
  s4[1] = 'G';
  s4[2] = 'B';
  int cnt = 0;
  for (int i = 1; i <= maxn - 1; i++) s1[i] = s4[cnt % 3], cnt++;
}
void work() {
  int n, k;
  cin >> n >> k;
  int ans = inf;
  cin >> s + 1;
  int t1, t2, t3, tt;
  t1 = t2 = t3 = 0;
  for (int i = 1; i <= k; i++) {
    if (s1[i] == s[i])
      t2++;
    else if (s1[i] == 'R') {
      if (s[i] == 'G') t3++;
      if (s[i] == 'B') t1++;
    } else if (s1[i] == 'G') {
      if (s[i] == 'R')
        t1++;
      else
        t3++;
    } else {
      if (s[i] == 'R')
        t3++;
      else
        t1++;
    }
  }
  ans = min(ans, t1 + t2);
  ans = min(ans, t2 + t3);
  ans = min(ans, t1 + t3);
  for (int i = k + 1; i <= n; i++) {
    if (s1[1] == s[i - k])
      t2--;
    else if (s1[1] == 'R') {
      if (s[i - k] == 'G') t3--;
      if (s[i - k] == 'B') t1--;
    } else if (s1[1] == 'G') {
      if (s[i - k] == 'R')
        t1--;
      else
        t3--;
    } else {
      if (s[i - k] == 'R')
        t3--;
      else
        t1--;
    }
    tt = t1;
    t1 = t3;
    t3 = t2;
    t2 = tt;
    if (s1[k] == s[i])
      t2++;
    else if (s1[k] == 'R') {
      if (s[i] == 'G') t3++;
      if (s[i] == 'B') t1++;
    } else if (s1[k] == 'G') {
      if (s[i] == 'R')
        t1++;
      else
        t3++;
    } else {
      if (s[i] == 'R')
        t3++;
      else
        t1++;
    }
    ans = min(ans, t1 + t2);
    ans = min(ans, t2 + t3);
    ans = min(ans, t1 + t3);
  }
  cout << ans << endl;
}
int main() {
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  ;
  init();
  int cas;
  cin >> cas;
  while (cas--) work();
}
