#include <bits/stdc++.h>
using namespace std;
int N, n, m, f[200005];
char s[200005], ss[3][5] = {"RGB", "GBR", "BRG"};
inline int rd() {
  int x = 0;
  char c = getchar();
  while ((c < '0' || c > '9')) c = getchar();
  while (c >= '0' && c <= '9')
    x = (x << 3) + (x << 1) + (c & 15), c = getchar();
  return x;
}
int main() {
  for (N = rd(); N--;) {
    scanf("%d%d%s", &n, &m, s + 1);
    int ans = n;
    for (int k = 0; k < 3; k++) {
      f[0] = 0;
      for (int i = 1; i <= n; i++) {
        if (s[i] == ss[k][i % 3])
          f[i] = f[i - 1];
        else
          f[i] = f[i - 1] + 1;
        if (i >= m) ans = min(ans, f[i] - f[i - m]);
      }
    }
    printf("%d\n", ans);
  }
}
