#include <bits/stdc++.h>
using namespace std;
int sm[212345][3];
int main() {
  int q, n, k, i, j, ans;
  string s, rgb = "RGB";
  cin >> q;
  while (q--) {
    cin >> n >> k;
    cin >> s;
    sm[0][0] = sm[0][1] = sm[0][2] = 0;
    for (i = 1; i <= n; i++) {
      for (j = 0; j < 3; j++) {
        sm[i][j % 3] = sm[i - 1][j % 3];
        if (s[i - 1] != rgb[(j + i - 1) % 3]) sm[i][j % 3]++;
      }
    }
    ans = 1e9;
    for (i = 0; i <= n - k; i++) {
      for (j = 0; j < 3; j++) {
        if (sm[i + k][j] - sm[i][j] < ans) {
          ans = sm[i + k][j] - sm[i][j];
        }
      }
    }
    cout << ans << endl;
  }
  return 0;
}
