#include <bits/stdc++.h>
using namespace std;
long long read(long long x = 0) {
  long long c, f(1);
  for (c = getchar(); !isdigit(c); c = getchar())
    if (c == '-') f = -f;
  for (; isdigit(c); c = getchar()) x = x * 10 + c - 48;
  return f * x;
}
char s[200010];
long long a[200010], cnt[200010][3];
int main() {
  long long T(read()), i, k, n, c, ans;
  while (T--) {
    n = read(), k = read();
    scanf("%s", s + 1);
    for (i = 1; i <= n; i++) {
      if (s[i] == 'R') a[i] = 0;
      if (s[i] == 'G') a[i] = 1;
      if (s[i] == 'B') a[i] = 2;
      (a[i] += 3 - i % 3) %= 3;
      for (c = 0; c < 3; c++) cnt[i][c] = cnt[i - 1][c];
      cnt[i][a[i]]++;
    }
    ans = 0x3f3f3f3f;
    for (c = 0; c < 3; c++)
      for (i = k; i <= n; i++) {
        ans = min(ans, k - (cnt[i][c] - cnt[i - k][c]));
      }
    printf("%lld\n", ans);
  }
  return 0;
}
