#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  ;
  int t;
  cin >> t;
  while (t--) {
    int n, k;
    string s;
    cin >> n >> k >> s;
    vector<long long> r(n, 0), g(n, 0), b(n, 0), a(n);
    for (int i = 0; i < n; i++) {
      if (s[i] == 'R')
        a[i] = 0;
      else if (s[i] == 'G')
        a[i] = 1;
      else
        a[i] = 2;
    }
    for (int i = 0; i < n; i++) {
      if (i % 3 != a[i]) r[i] = 1;
      if ((i + 1) % 3 != a[i]) g[i] = 1;
      if ((i + 2) % 3 != a[i]) b[i] = 1;
    }
    int cnt = 0, ans = 1e9;
    for (int i = 0; i < n; i++) {
      cnt += r[i];
      if (i - k >= 0) cnt -= r[i - k];
      if (i - k + 1 >= 0) ans = min(ans, cnt);
    }
    cnt = 0;
    for (int i = 0; i < n; i++) {
      cnt += g[i];
      if (i - k >= 0) cnt -= g[i - k];
      if (i - k + 1 >= 0) ans = min(ans, cnt);
    }
    cnt = 0;
    for (int i = 0; i < n; i++) {
      cnt += b[i];
      if (i - k >= 0) cnt -= b[i - k];
      if (i - k + 1 >= 0) ans = min(ans, cnt);
    }
    cout << ans << endl;
  }
  return 0;
}
