#include <bits/stdc++.h>
using namespace std;
int main() {
  int q, n, k, i, j, pos, ans;
  cin >> q;
  vector<char> s(200004);
  string t;
  s[0] = 'R';
  for (i = 1; i < 200003; i++) {
    if (s[i - 1] == 'R') s[i] = 'G';
    if (s[i - 1] == 'G') s[i] = 'B';
    if (s[i - 1] == 'B') s[i] = 'R';
  }
  while (q--) {
    cin >> n >> k >> t;
    ans = 100000000;
    for (i = 0; i < 3; i++) {
      pos = 0;
      vector<int> sum(n, 0);
      for (j = i; j < i + n; j++) {
        if (s[j] != t[pos]) sum[pos]++;
        pos++;
      }
      for (j = 1; j < n; j++) sum[j] += sum[j - 1];
      ans = min(ans, sum[k - 1]);
      for (j = k; j < n; j++) ans = min(ans, sum[j] - sum[j - k]);
    }
    cout << ans << '\n';
  }
  return 0;
}
