#include <bits/stdc++.h>
#pragma GCC target("arch=ivybridge")
using namespace ::std;
template <class T>
istream& operator>>(istream& I, vector<T>& v) {
  for (T& e : v) I >> e;
  return I;
}
template <class T>
ostream& operator<<(ostream& O, const vector<T>& v) {
  for (const T& e : v) O << e << ' ';
  return O;
}
string p;
void _main() {
  int n, k;
  cin >> n >> k;
  string str;
  cin >> str;
  vector<int> a(n), b(n), c(n);
  while (p.size() <= n + 10) p += "RGB";
  for (int i = 0; i < n; i++) {
    if (str[i] != p[i]) a[i]++;
    if (i) a[i] += a[i - 1];
  }
  p.erase(p.begin());
  for (int i = 0; i < n; i++) {
    if (str[i] != p[i]) b[i]++;
    if (i) b[i] += b[i - 1];
  }
  p.erase(p.begin());
  for (int i = 0; i < n; i++) {
    if (str[i] != p[i]) c[i]++;
    if (i) c[i] += c[i - 1];
  }
  auto pre = [](int i, int j, vector<int>& v) {
    if (i == 0) return v[j];
    return v[j] - v[i - 1];
  };
  int ans = n;
  for (int i = 0; i + k - 1 < n; i++) {
    ans = min(ans, pre(i, i + k - 1, a));
    ans = min(ans, pre(i, i + k - 1, b));
    ans = min(ans, pre(i, i + k - 1, c));
  }
  cout << ans << '\n';
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int _t = 1;
  cin >> _t;
  while (_t--) _main();
  return 0;
}
