#include <bits/stdc++.h>
using namespace std;
int t, i, j, n, k, ans, a[200005], sum[200005];
char s[200005];
char s1[] = "RGB";
void solve() {
  scanf("%d%d", &n, &k);
  scanf("%s", s + 1);
  ans = 0;
  for (j = 0; j <= 2; j++) {
    sum[1] = 0;
    for (i = 1; i <= n; i++) {
      if (s[i] == s1[(i + j) % 3])
        sum[i + 1] = sum[i] + 1;
      else
        sum[i + 1] = sum[i];
      if (i + 1 >= k) ans = max(ans, sum[i + 1] - sum[i + 1 - k]);
    }
  }
  ans = k - ans;
  printf("%d\n", ans);
}
int main() {
  scanf("%d", &t);
  while (t--) solve();
  return 0;
}
