#include <bits/stdc++.h>
using namespace std;
long long fun(string &s, long long k, string &temp) {
  long long val = INT_MAX;
  long long cur = 0;
  for (long long i = 0; i < k; i++) {
    if (s[i] != temp[i]) cur++;
  }
  val = min(val, cur);
  for (long long i = k; i < s.length(); i++) {
    if (s[i] != temp[i]) {
      cur++;
    }
    if (s[i - k] != temp[i - k]) {
      cur--;
    }
    val = min(val, cur);
  }
  return val;
}
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  long long t;
  cin >> t;
  while (t--) {
    long long n, k;
    cin >> n >> k;
    string s;
    cin >> s;
    string temp1;
    string temp2;
    string temp3;
    for (long long i = 0; i < s.length(); i++) {
      if (i % 3 == 0) {
        temp1.push_back('R');
        temp2.push_back('G');
        temp3.push_back('B');
      } else if (i % 3 == 1) {
        temp1.push_back('G');
        temp2.push_back('B');
        temp3.push_back('R');
      } else {
        temp1.push_back('B');
        temp2.push_back('R');
        temp3.push_back('G');
      }
    }
    long long ans = INT_MAX;
    ans = min(ans, fun(s, k, temp1));
    ans = min(ans, fun(s, k, temp2));
    ans = min(ans, fun(s, k, temp3));
    cout << ans << endl;
  }
  return 0;
}
