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