#include <bits/stdc++.h>
using namespace std;
const string t = "RGB";
struct rgb_string : vector<int> {
  int min_mismatches;
  rgb_string(int n, int k, const string &s) : min_mismatches(INT_MAX) {
    resize(n + 1, 0);
    for (int j = 0; j < 3 and min_mismatches > 0; ++j) {
      for (int i = 0, u = 1, v = j; i < n; i = u++, ++v %= 3)
        at(u) = at(i) + (s[i] != t[v]);
      for (int i = 0, u = k; u <= n and min_mismatches > 0; ++i, ++u)
        min_mismatches = min(min_mismatches, at(u) - at(i));
    }
  }
};
int main() {
  int q, n, k;
  string s;
  ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr),
      cin >> q;
  while (q--)
    cin >> n >> k >> s, cout << rgb_string(n, k, s).min_mismatches << '\n';
}
