#include <bits/stdc++.h>
using namespace std;
const int N = 1000005;
char kel[3] = {'R', 'G', 'B'};
string s;
int f[N][3][3], n, k;
void work() {
  int ans = 0;
  scanf("%d%d", &n, &k);
  cin >> s;
  s = ' ' + s;
  for (int k1 = 0; k1 < 3; k1++) {
    for (int k2 = 0; k2 < 3; k2++) {
      for (int i = 1; i <= n; i++) {
        f[i][k1][k2] =
            f[i - 1][k1][k2] + ((kel[k1] == s[i]) && ((i - k2) % 3 == 0));
      }
    }
  }
  for (int i = 1; i <= n - k + 1; i++) {
    for (int j = 0; j < 3; j++) {
      int tmp = 0, ii = i + k - 1;
      for (int k = 0; k < 3; k++) {
        int k1 = (i + k) % 3, k2 = (j + k) % 3;
        int val = f[ii][k2][k1] - f[i - 1][k2][k1];
        tmp += val;
      }
      ans = max(ans, tmp);
    }
  }
  printf("%d\n", k - ans);
}
int main() {
  int t;
  scanf("%d", &t);
  while (t--) {
    work();
  }
  return 0;
}
