#include <bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f;
const int N = 2e5 + 10;
char str[N];
char a[] = {'R', 'G', 'B'};
int num[N];
int n, k;
int solve(int x) {
  for (int i = 0; i <= n; i++) num[i] = 0;
  for (int i = 1; i <= n; i++) {
    if (str[i] != a[x])
      num[i] = num[i - 1] + 1;
    else
      num[i] = num[i - 1];
    x = (x + 1) % 3;
  }
  int ans = INF;
  int sum = 0;
  for (int i = 1; i <= n - k + 1; i++) {
    ans = min(ans, num[i + k - 1] - sum);
    if (num[i] != num[i - 1]) sum = num[i];
  }
  return ans;
}
int main() {
  int t;
  scanf("%d", &t);
  while (t--) {
    scanf("%d%d\n", &n, &k);
    scanf("%s", str + 1);
    int ans = min(solve(0), min(solve(1), solve(2)));
    printf("%d\n", ans);
  }
  return 0;
}
