#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int q, n, k, ans;
  string s;
  cin >> q;
  for (int u = 0; u < q; ++u) {
    cin >> n >> k >> s;
    vector<int> rgb(n + 1, 0);
    vector<int> gbr(n + 1, 0);
    vector<int> brg(n + 1, 0);
    for (int i = 0; i < n; ++i) {
      rgb[i + 1] = rgb[i];
      gbr[i + 1] = gbr[i];
      brg[i + 1] = brg[i];
      if (i % 3 == 0) {
        if (s[i] == 'R')
          ++rgb[i + 1];
        else if (s[i] == 'G')
          ++gbr[i + 1];
        else
          ++brg[i + 1];
      } else if (i % 3 == 1) {
        if (s[i] == 'G')
          ++rgb[i + 1];
        else if (s[i] == 'B')
          ++gbr[i + 1];
        else
          ++brg[i + 1];
      } else {
        if (s[i] == 'B')
          ++rgb[i + 1];
        else if (s[i] == 'R')
          ++gbr[i + 1];
        else
          ++brg[i + 1];
      }
    }
    ans = k;
    for (int i = k; i <= n; ++i) {
      if (k - (rgb[i] - rgb[i - k]) < ans) ans = k - (rgb[i] - rgb[i - k]);
      if (k - (gbr[i] - gbr[i - k]) < ans) ans = k - (gbr[i] - gbr[i - k]);
      if (k - (brg[i] - brg[i - k]) < ans) ans = k - (brg[i] - brg[i - k]);
    }
    cout << ans << '\n';
  }
  return 0;
}
