#include <bits/stdc++.h>
using namespace std;
const int INF = 1000000010;
const int N = 200010;
template <typename T>
inline T sqr(T x) {
  return (x * x);
}
int main() {
  int q;
  string s;
  string str = "RGB";
  cin >> q;
  while (q--) {
    int n, k;
    int r = 0, g = 0, b = 0, ans = INF;
    cin >> n >> k >> s;
    for (int i = 0; i < k; i++) {
      if (s[i] != str[i % 3]) {
        r++;
      }
      if (s[i] != str[(i + 1) % 3]) {
        g++;
      }
      if (s[i] != str[(i + 2) % 3]) {
        b++;
      }
    }
    ans = min(min(ans, r), min(g, b));
    for (int i = k; i < n; i++) {
      if (s[i] != str[i % 3]) {
        r++;
      }
      if (s[i] != str[(i + 1) % 3]) {
        g++;
      }
      if (s[i] != str[(i + 2) % 3]) {
        b++;
      }
      if (s[i - k] != str[(i - k) % 3]) {
        r--;
      }
      if (s[i - k] != str[(i - k + 1) % 3]) {
        g--;
      }
      if (s[i - k] != str[(i - k + 2) % 3]) {
        b--;
      }
      ans = min(min(ans, r), min(g, b));
    }
    cout << ans << endl;
  }
  return 0;
}
