#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
const int MAX = (int)(2e5) + 7;
const int MOD = (int)(1e9) + 7;
const int INF_I = (int)(1e9) + 7;
const ll INF_LL = (ll)(1e18);
template <typename T>
void info(T& v) {
  for (const auto& i : v) cout << i << ' ';
  cout << endl;
}
template <typename T>
bool chkmin(T& a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <typename T>
bool chkmax(T& a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
void boot_up() {
  cin.tie(nullptr);
  cout.tie(nullptr);
  ios_base::sync_with_stdio(false);
}
int q;
int n, k;
void solve();
signed main() {
  boot_up();
  cin >> q;
  while (cin >> n >> k) solve();
}
void solve() {
  string s;
  cin >> s;
  int curr_pos = 0;
  string a = "", b = "", c = "";
  vector<string> mask = {"R", "G", "B"};
  for (int i = 0; i < n; ++i) {
    a += mask[curr_pos % 3];
    b += mask[(curr_pos + 1) % 3];
    c += mask[(curr_pos + 2) % 3];
    curr_pos = (curr_pos + 1) % 3;
  }
  vector<int> pref_a(n);
  vector<int> pref_b(n);
  vector<int> pref_c(n);
  pref_a[0] = (s[0] == 'R' ? 0 : 1);
  pref_b[0] = (s[0] == 'G' ? 0 : 1);
  pref_c[0] = (s[0] == 'B' ? 0 : 1);
  for (int i = 1; i < n; ++i) {
    pref_a[i] = pref_a[i - 1] + 1 - (int)(s[i] == a[i]);
    pref_b[i] = pref_b[i - 1] + 1 - (int)(s[i] == b[i]);
    pref_c[i] = pref_c[i - 1] + 1 - (int)(s[i] == c[i]);
  }
  int ans = INF_I;
  for (int i = 0; i < n - k + 1; ++i) {
    chkmin(ans, pref_a[i + k - 1] - (i == 0 ? 0 : pref_a[i - 1]));
    chkmin(ans, pref_b[i + k - 1] - (i == 0 ? 0 : pref_b[i - 1]));
    chkmin(ans, pref_c[i + k - 1] - (i == 0 ? 0 : pref_c[i - 1]));
  }
  cout << ans << '\n';
}
