#include <bits/stdc++.h>
using namespace std;
string pat[] = {"RGB", "GBR", "BRG"};
const int inf = 10000000;
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  int t, n, kth;
  string s;
  cin >> t;
  while (t--) {
    cin >> n >> kth >> s;
    int ans = inf;
    for (int k = 0, gon = 3; k < gon; ++k) {
      int acum[n + 1], mx = 0;
      vector<int> v;
      string p = pat[k];
      acum[0] = 0;
      for (int i = 0, gon = ((int)s.size()); i < gon; ++i) {
        acum[i + 1] = (s[i] == p[i % 3]) + acum[i];
      }
      for (int i = 1; i + kth - 1 <= n; i++) {
        mx = max(mx, acum[i + kth - 1] - acum[i - 1]);
      }
      ans = min(ans, kth - mx);
    }
    cout << ans << "\n";
  }
}
