#include <bits/stdc++.h>
inline int read() {
  int f = 1, x = 0;
  char ch = getchar();
  while (!(ch >= '0' && ch <= '9')) {
    if (ch == '-') f *= -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    x = x * 10 + ch - '0';
    ch = getchar();
  }
  return f * x;
}
int q, n, k, ans, f[200000 + 1], p[200000 + 1], v[3];
char s[200000 + 1];
int main() {
  q = read();
  while (q--) {
    n = read(), k = read();
    scanf("%s", s + 1);
    for (int i = 1; i <= n; i++) {
      if (s[i] == 'R')
        f[i] = 2;
      else if (s[i] == 'G')
        f[i] = 1;
      else
        f[i] = 0;
      p[i] = (f[i] + i) % 3;
    }
    for (int i = 1; i < k; i++) v[p[i]]++;
    ans = 2147483647;
    for (int i = k; i <= n; i++) {
      v[p[i]]++;
      for (int j = 0; j <= 2; j++) ans = std::min(ans, k - v[j]);
      v[p[i - k + 1]]--;
    }
    for (int i = n - k + 2; i <= n; i++) v[p[i]]--;
    printf("%d\n", ans);
  }
}
