#include <bits/stdc++.h>
using namespace std;
int q;
char str[200000];
char rgb[4] = "RGB";
int main() {
  scanf("%d", &q) == 0;
  for (decltype(0) z = 0; z < q; z++) {
    int n, k;
    scanf("%d", &n) == 0, scanf("%d", &k) == 0;
    scanf("%s", str) == 0;
    int r = k;
    for (decltype(0) o = 0; o < 3; o++) {
      int si = 0;
      int ei = 0;
      int cost = 0;
      while (ei < n) {
        while (ei - si < k) {
          if (rgb[(ei + o) % 3] != str[ei]) cost++;
          ei++;
        }
        r = min(r, cost);
        if (rgb[(si + o) % 3] != str[si]) cost--;
        si++;
      }
    }
    printf("%d\n", r);
  }
}
int main2() {
  scanf("%d", &q) == 0;
  for (decltype(0) z = 0; z < q; z++) {
    int n, k;
    scanf("%d", &n) == 0, scanf("%d", &k) == 0;
    scanf("%s", str) == 0;
    int r = k;
    for (decltype(0) m = 0; m < 3; m++) {
      for (decltype(0) i = 0; i < n - k + 1; i++) {
        int cost = 0;
        for (decltype(0) j = 0; j < k; j++) {
          if (str[i + j] != rgb[(m + j) % 3]) cost++;
        }
        r = min(r, cost);
      }
    }
    printf("%d\n", r);
  }
}
