#include <bits/stdc++.h>
using namespace std;
const int N = 200005;
const char c[3][4] = {"RGB", "GBR", "BRG"};
int sum[3][N];
char s[N];
int main() {
  int n, k, T;
  scanf("%d", &T);
  while (T--) {
    scanf("%d%d", &n, &k);
    sum[0][0] = sum[1][0] = sum[2][0] = 0;
    scanf("%s", s);
    if (k == 1) {
      printf("0\n");
      continue;
    }
    for (int i = 0; i < 3; i++) {
      for (int j = 0; j < n; j++) {
        if (s[j] != c[i][j % 3]) {
          sum[i][j + 1] = sum[i][j] + 1;
        } else {
          sum[i][j + 1] = sum[i][j];
        }
      }
    }
    int ans = N;
    for (int p = 0; p < 3; p++) {
      for (int i = 1; i + k - 1 <= n; i++) {
        int j = i + k - 1;
        ans = min(ans, sum[p][j] - sum[p][i - 1]);
      }
    }
    printf("%d\n", ans);
  }
  return 0;
}
