#include <bits/stdc++.h>
#pragma optimize("Ofast")
using namespace std;
using ll = long long;
const int N = 2e5 + 5;
int n, k;
int solve(int x, string s) {
  string t = "";
  if (x == 1)
    for (int i = 0; i < n / 3 + 1; ++i) t += "RGB";
  else if (x == 2)
    for (int i = 0; i < n / 3 + 1; ++i) t += "GBR";
  else
    for (int i = 0; i < n / 3 + 1; ++i) t += "BRG";
  int ans = 2e5 + 5, l = 0, cnt = 0;
  for (int i = 0; i < k; ++i) {
    if (t[i] != s[i]) ++cnt;
  }
  if (cnt < ans) ans = cnt;
  for (int i = k; i < n; ++i) {
    if (t[l] != s[l]) {
      --cnt;
    }
    ++l;
    if (t[i] != s[i]) {
      ++cnt;
    }
    if (cnt < ans) ans = cnt;
  }
  return ans;
}
int main() {
  ios_base::sync_with_stdio(0);
  int q;
  cin >> q;
  while (q--) {
    string s;
    cin >> n >> k >> s;
    int ans = 2e5;
    ans = min(ans, solve(1, s));
    ans = min(ans, solve(2, s));
    ans = min(ans, solve(3, s));
    cout << ans << "\n";
  }
  return 0;
}
