#include <bits/stdc++.h>
using namespace std;
char a[200010];
int dp[200000][3];
int main() {
  int T;
  scanf("%d", &T);
  while (T--) {
    int q, k;
    scanf("%d%d", &q, &k);
    scanf("%s", a);
    for (int i = 0; i < q; i++) {
      if (a[i] == 'R') {
        dp[i + 1][0] = dp[i][2];
        dp[i + 1][1] = dp[i][0] + 1;
        dp[i + 1][2] = dp[i][1] + 1;
      }
      if (a[i] == 'G') {
        dp[i + 1][0] = dp[i][2] + 1;
        dp[i + 1][1] = dp[i][0];
        dp[i + 1][2] = dp[i][1] + 1;
      }
      if (a[i] == 'B') {
        dp[i + 1][0] = dp[i][2] + 1;
        dp[i + 1][1] = dp[i][0] + 1;
        dp[i + 1][2] = dp[i][1];
      }
    }
    int ans = 0x3f3f3f3f;
    for (int i = 0; i < q - k + 1; i++) {
      for (int j = 0; j < 3; j++) {
        int tmp = (j + 3000000 - k) % 3;
        ans = min(ans, dp[i + k][j] - dp[i][tmp]);
      }
    }
    printf("%d\n", ans);
  }
}
