#include <bits/stdc++.h>
using namespace std;
const long long int N = 2e5 + 5;
string x = "RGB", y = "GBR", z = "BRG";
void solve() {
  long long int n, k;
  cin >> n >> k;
  string s;
  cin >> s;
  vector<long long int> a(3);
  for (long long int i = 0; i < k; i++) {
    if (s[i] != x[i % 3]) {
      a[0]++;
    }
    if (s[i] != y[i % 3]) {
      a[1]++;
    }
    if (s[i] != z[i % 3]) {
      a[2]++;
    }
  }
  long long int ans = *min_element(a.begin(), a.end());
  for (long long int i = 1; i + k <= n; i++) {
    if (s[i - 1] != x[0]) {
      a[0]--;
    }
    if (s[i - 1] != y[0]) {
      a[1]--;
    }
    if (s[i - 1] != z[0]) {
      a[2]--;
    }
    long long int p = a[2], q = a[0], r = a[1];
    long long int ind = i + k - 1;
    if (s[ind] != x[(k - 1) % 3]) {
      p++;
    }
    if (s[ind] != y[(k - 1) % 3]) {
      q++;
    }
    if (s[ind] != z[(k - 1) % 3]) {
      r++;
    }
    a[0] = p, a[1] = q, a[2] = r;
    ans = min(ans, *min_element(a.begin(), a.end()));
  }
  cout << ans << "\n";
}
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  long long int t = 1;
  cin >> t;
  while (t--) solve();
  return 0;
}
