#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 10;
int dp[maxn][3], n, k;
char ss[maxn], bz[maxn][3];
void Init() {
  char *ch = "RBG";
  for (int i = 1; i < maxn; i++) {
    for (int j = 0; j < 3; j++) {
      bz[i][j] = ch[(i + j) % 3];
    }
  }
}
int main(void) {
  int T;
  Init();
  scanf("%d", &T);
  while (T--) {
    scanf("%d%d", &n, &k);
    scanf("%s", ss + 1);
    for (int i = 1; i <= n; i++) {
      for (int j = 0; j < 3; j++) {
        if (bz[i][(i + j) % 3] != ss[i])
          dp[i][j] = 1;
        else
          dp[i][j] = 0;
      }
    }
    int ans = n;
    for (int j = 0; j < 3; j++) {
      int tp = 0;
      for (int i = 1; i <= k; i++) tp += dp[i][j];
      if (ans > tp) ans = tp;
      for (int i = k + 1; i <= n; i++) {
        tp += dp[i][j];
        tp -= dp[i - k][j];
        if (ans > tp) ans = tp;
      }
    }
    printf("%d\n", ans);
  }
  return 0;
}
