#include <bits/stdc++.h>
using namespace std;
namespace SOL {
const double EPS = 1e-6;
const double PI = acos(-1.0);
const int INF = 0x3f3f3f3f;
const long long LINF = 0x7f7f7f7f7f7f7f7f;
const int MOD = (int)1e9 + 7;
const int MAXN = (int)1e6 + 10;
int q, n, k;
int ans;
void Find(string t, string &s) {
  int l = 1, r = 1, res = 0;
  while (l <= n) {
    while (r <= n && r - l < k) {
      res += (s[r] == t[r % 3 == 0 ? 3 : r % 3]);
      ++r;
    }
    if (r - l < k) break;
    ans = max(ans, res);
    res -= (s[l] == t[l % 3 == 0 ? 3 : l % 3]);
    ++l;
  }
}
void solve() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> q;
  while (q-- > 0) {
    ans = 0;
    string s, st;
    cin >> n >> k >> st;
    s.push_back('0');
    s += st;
    Find(" RGB", s);
    Find(" GBR", s);
    Find(" BRG", s);
    if (ans >= k)
      cout << 0 << '\n';
    else
      cout << (k - ans) << '\n';
  }
}
}  // namespace SOL
int main() {
  SOL::solve();
  return 0;
}
