#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 20;
int a[N];
int main() {
  int q;
  cin >> q;
  char rgb[N];
  for (int i = 0; i <= 2e5 + 7; i += 3) {
    rgb[i] = 'R', rgb[i + 1] = 'G', rgb[i + 2] = 'B';
  }
  while (q--) {
    int n, k, c = 0;
    scanf("%d%d", &n, &k);
    char s[N];
    scanf("%s", s);
    int ans = 0x3f3f3f3f;
    while (c < 3) {
      for (int j = 0; j < n; j++) {
        a[j] = 0;
        if (s[j] == rgb[c + j])
          a[j] = 0;
        else
          a[j] = 1;
        if (j != 0) a[j] = a[j - 1] + a[j];
      }
      c++;
      for (int j = k - 1; j < n; j++) {
        if (j == k - 1)
          ans = min(ans, a[j]);
        else
          ans = min(ans, a[j] - a[j - k]);
      }
    }
    printf("%d\n", ans);
  }
  return 0;
}
