#include <bits/stdc++.h>
using namespace std;
const int mx = 2e5 + 7;
char s[mx];
int sum[mx][3][3];
int main() {
  unordered_map<char, int> mp;
  mp['R'] = 0;
  mp['G'] = 1;
  mp['B'] = 2;
  int t, n, k;
  scanf("%d", &t);
  while (t--) {
    scanf("%d%d", &n, &k);
    scanf("%s", s + 1);
    if (k == 1) {
      printf("0\n");
      continue;
    }
    if (k == 2) {
      bool ok = 0;
      for (int i = 2; i <= n; i++) {
        if (s[i] == 'R' && s[i - 1] == 'B') {
          ok = 1;
          break;
        }
        if (s[i] == 'G' && s[i - 1] == 'R') {
          ok = 1;
          break;
        }
        if (s[i] == 'B' && s[i - 1] == 'G') {
          ok = 1;
          break;
        }
      }
      if (ok)
        printf("0\n");
      else
        printf("1\n");
      continue;
    }
    for (int i = 1; i <= n; i++) {
      for (int j = 0; j < 3; j++)
        for (int r = 0; r < 3; r++) sum[i][j][r] = 0;
    }
    for (int i = 1; i <= n; i++) sum[i][mp[s[i]]][i % 3]++;
    for (int i = 1; i <= n; i++) {
      for (int j = 0; j < 3; j++)
        for (int r = 0; r < 3; r++) sum[i][j][r] += sum[i - 1][j][r];
    }
    int ans = 0x3f3f3f3f;
    for (int i = k; i <= n; i++) {
      for (int j = 0; j < 3; j++) {
        int num = 0;
        num += sum[i][j][i % 3] - sum[i - k][j][i % 3];
        if (k >= 3)
          num += sum[i][(j + 2) % 3][(i + 2) % 3] -
                 sum[i - k][(j + 2) % 3][(i + 2) % 3];
        if (k >= 2)
          num += sum[i][(j + 1) % 3][(i + 1) % 3] -
                 sum[i - k][(j + 1) % 3][(i + 1) % 3];
        if (ans > k - num) {
          ans = k - num;
        }
      }
    }
    printf("%d\n", ans);
  }
  return 0;
}
