#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 10;
const long long mod = 1e9 + 7;
bool num[maxn];
char word[maxn];
char key[3] = {'R', 'G', 'B'};
int n, k;
int cal(int x) {
  int res = 1e9, now = 0;
  for (int i = 1; i <= n; i++) {
    num[i] = key[(i + x) % 3] != word[i];
    if (i < k) now += num[i];
    if (i >= k) now = now + num[i] - num[i - k], res = min(res, now);
  }
  return res;
}
int main() {
  int T;
  scanf("%d", &T);
  while (T--) {
    scanf("%d %d", &n, &k);
    scanf("%s", word + 1);
    printf("%d\n", min(cal(0), min(cal(1), cal(2))));
  }
  return 0;
}
