#include <bits/stdc++.h>
using namespace std;
char r[5];
int main() {
  int q;
  scanf("%d", &q);
  r[0] = 'R';
  r[1] = 'G';
  r[2] = 'B';
  while (q--) {
    int n, k;
    int ans = 0x3f3f3f3f;
    scanf("%d%d", &n, &k);
    char s[200005];
    scanf("%s", s);
    for (int i = 0; i < 3; i++) {
      int res[200005];
      int cur = 0;
      for (int j = 0; j < n; j++) {
        if (s[j] == r[(j + i) % 3])
          res[j] = 0;
        else
          res[j] = 1;
        cur += res[j];
        if (j >= k) cur -= res[j - k];
        if (j >= k - 1) ans = min(ans, cur);
      }
    }
    printf("%d\n", ans);
  }
}
