#include <bits/stdc++.h>
using namespace std;
int main() {
  string r = "RGB";
  int t;
  cin >> t;
  while (t--) {
    int n, k;
    string s;
    cin >> n >> k >> s;
    int ans = INT_MAX;
    for (int offset = 0; offset < 3; ++offset) {
      vector<int> res(n);
      int cnt = 0;
      for (int i = 0; i < n; ++i) {
        res[i] = (s[i] == r[(offset + i) % 3]);
        cnt += res[i];
        if (i >= k) cnt -= res[i - k];
        if (i >= k - 1) ans = min(ans, k - cnt);
      }
    }
    cout << ans << endl;
  }
  return 0;
}
