#include <bits/stdc++.h>
using namespace std;
long long q, n, k;
string s, s1, s2, s3;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  string tmp = "RGB";
  for (int i = 0; i < 100000; i++) s1 += tmp;
  s2 = "B" + s1;
  s3 = "G" + s2;
  cin >> q;
  while (q--) {
    cin >> n >> k >> s;
    s = " " + s;
    vector<long long> f1(n + 1), f2(n + 1), f3(n + 1);
    for (int i = 1; i <= n; i++) {
      f1[i] = f1[i - 1];
      f2[i] = f2[i - 1];
      f3[i] = f3[i - 1];
      if (s[i] != s1[i - 1]) f1[i]++;
      if (s[i] != s2[i - 1]) f2[i]++;
      if (s[i] != s3[i - 1]) f3[i]++;
    }
    long long ans = 1e18;
    for (int i = 1; i + k - 1 <= n; i++) {
      ans = min(ans, f1[i + k - 1] - f1[i - 1]);
      ans = min(ans, f2[i + k - 1] - f2[i - 1]);
      ans = min(ans, f3[i + k - 1] - f3[i - 1]);
    }
    cout << ans << "\n";
  }
  return 0;
}
