#include <bits/stdc++.h>
using namespace std;
const int mod = 1e9 + 7;
const int inf = 1e9 + 7;
const int maxn = 2e5 + 10;
int res[maxn];
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  const string t = "RGB";
  int T;
  cin >> T;
  int n, k;
  string str;
  while (T--) {
    cin >> n >> k >> str;
    int minn = inf;
    for (int flag = 0; flag < 3; flag++) {
      int cnt = 0;
      for (int i = 0; i < k; i++) {
        if (str[i] != t[(i + flag) % 3]) cnt++;
      }
      if (cnt < minn) minn = cnt;
      for (int i = k; i < n; i++) {
        if (str[i] != t[(i + flag) % 3]) cnt++;
        if (str[i - k] != t[(i - k + flag) % 3]) cnt--;
        if (cnt < minn) minn = cnt;
      }
    }
    cout << minn << endl;
  }
  return 0;
}
