#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 5;
char s[maxn], x[3][maxn], mp[4] = "RGB";
int cnt[3][maxn];
void init() {
  for (int i = 1; i <= maxn - 5; i++) {
    x[0][i] = mp[i % 3];
    x[1][i] = mp[(i + 1) % 3];
    x[2][i] = mp[(i + 2) % 3];
  }
}
int main() {
  int t, n, k, d;
  init();
  scanf("%d", &t);
  while (t--) {
    int ans = maxn;
    scanf("%d %d %s", &n, &k, s + 1);
    for (int i = 1; i <= n; i++) {
      for (int j = 0; j < 3; j++) {
        cnt[j][i] = cnt[j][i - 1];
        if (x[j][i] != s[i]) cnt[j][i]++;
        if (i >= k) ans = min(ans, cnt[j][i] - cnt[j][i - k]);
      }
    }
    printf("%d\n", ans);
  }
  return 0;
}
