#include <bits/stdc++.h>
using namespace std;
int a[21021020];
char aa[] = "RGB";
char bb[] = "GBR";
char cc[] = "BRG";
int sa[2102020];
int sb[2020220];
int sc[2020200];
int main() {
  int T;
  cin >> T;
  while (T--) {
    int n, m;
    cin >> n >> m;
    getchar();
    char c;
    char last;
    a[1] = 1;
    int ju = 1;
    int ans = -111, num = 1;
    int ll, rr;
    int u = 0;
    for (int i = 1; i <= n; i++) {
      scanf("%c", &c);
      if (c == aa[u]) {
        sa[i] = sa[i - 1] + 1;
      } else
        sa[i] = sa[i - 1];
      if (c == bb[u]) {
        sb[i] = sb[i - 1] + 1;
      } else
        sb[i] = sb[i - 1];
      if (c == cc[u]) {
        sc[i] = sc[i - 1] + 1;
      } else
        sc[i] = sc[i - 1];
      u++;
      if (u > 2) u = 0;
    }
    for (int i = m; i <= n; i++) {
      u = max(sa[i] - sa[i - m], sb[i] - sb[i - m]);
      ans = max(ans, u);
      ans = max(ans, sc[i] - sc[i - m]);
    }
    cout << m - ans << endl;
  }
  return 0;
}
