#include <bits/stdc++.h>
using namespace std;
int main() {
  int q;
  cin >> q;
  string str = "RGB";
  while (q--) {
    int n, k, ans = INT_MAX;
    string s;
    cin >> n >> k >> s;
    for (int i = 0; i < 3; i++) {
      vector<int> v(n);
      int cur = 0;
      for (int j = 0; j < n; j++) {
        v[j] = (s[j] != str[(i + j) % 3]);
        cur += v[j];
        if (j >= k) cur -= v[j - k];
        if (j >= k - 1) ans = min(ans, cur);
      }
    }
    cout << ans << "\n";
  }
}
