#include <bits/stdc++.h>
using namespace std;
int main() {
  int q;
  cin >> q;
  for (int Q = 0; Q < q; Q++) {
    int n, k;
    cin >> n >> k;
    vector<int> rw = vector<int>(n);
    vector<int> gw = vector<int>(n);
    vector<int> bw = vector<int>(n);
    int rws = 0, gws = 0, bws = 0;
    string s;
    cin >> s;
    vector<char> correct = {'R', 'G', 'B'};
    for (int i = 0; i < n; i++) {
      if (s[i] != correct[i % 3]) rws++;
      if (s[i] != correct[(1 + i) % 3]) gws++;
      if (s[i] != correct[(2 + i) % 3]) bws++;
      rw[i] = rws;
      gw[i] = gws;
      bw[i] = bws;
    }
    int ans = min(bw[k - 1], min(rw[k - 1], gw[k - 1]));
    for (int i = k; i < n; i++) {
      ans = min(ans, rw[i] - rw[i - k]);
      ans = min(ans, gw[i] - gw[i - k]);
      ans = min(ans, bw[i] - bw[i - k]);
    }
    cout << ans << endl;
  }
  return 0;
}
