#include <bits/stdc++.h>
using namespace std;
const int prime = 999983;
const int INF = 0x7FFFFFFF;
const long long INFF = 0x7FFFFFFFFFFFFFFF;
const double pi = acos(-1.0);
const double inf = 1e18;
const double eps = 1e-6;
const long long mod = 1e9 + 7;
long long qpow(long long a, long long b) {
  long long s = 1;
  while (b > 0) {
    if (b & 1) s = s * a % mod;
    a = a * a % mod;
    b >>= 1;
  }
  return s;
}
long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; }
int dr[2][4] = {1, -1, 0, 0, 0, 0, -1, 1};
const int maxn = 2e5 + 10;
char ar[maxn];
int a[maxn];
int sum[maxn];
int main(void) {
  int q;
  cin >> q;
  while (q--) {
    int n, k;
    scanf("%d%d", &n, &k);
    scanf("%s", ar);
    for (int i = 0; i < n; ++i) {
      if (ar[i] == 'R')
        a[i] = 0;
      else if (ar[i] == 'G')
        a[i] = 1;
      else
        a[i] = 2;
    }
    int ans = k;
    for (int i = 0; i < 3; ++i) {
      for (int j = 0; j < n; ++j) {
        sum[j] = 0;
        if (j == 0)
          sum[j] = (a[j] == (i + j) % 3 ? 0 : 1);
        else
          sum[j] = sum[j - 1] + (a[j] == (i + j) % 3 ? 0 : 1);
      }
      for (int j = k - 1; j < n; ++j)
        ans = min(ans, sum[j] - (j - k == -1 ? 0 : sum[j - k]));
    }
    printf("%d\n", ans);
  }
  return 0;
}
