#include <bits/stdc++.h>
#pragma GCC optimize(3)
using namespace std;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int q;
  cin >> q;
  while (q--) {
    int n, k;
    cin >> n >> k;
    string s;
    cin >> s;
    int best = k;
    char value[] = {'R', 'G', 'B'};
    for (int type = 0; type < 3; type++) {
      int errcount = 0;
      for (int i = 0; i < k; i++) {
        if (s[i] != value[(type + i) % 3]) errcount++;
      }
      best = min(best, errcount);
      for (int i = k; i < n; i++) {
        if (s[i] != value[(type + i) % 3]) errcount++;
        if (s[i - k] != value[(type + i - k) % 3]) errcount--;
        best = min(best, errcount);
      }
    }
    cout << best << endl;
  }
  return 0;
}
