#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int q;
  cin >> q;
  while (q--) {
    int N, K;
    cin >> N >> K;
    string S;
    cin >> S;
    int best = K;
    int start = 0;
    char vals[3] = {'R', 'G', 'B'};
    for (int type = 0; type < 3; type++) {
      int errCount = 0;
      for (int i = 0; i < K; i++) {
        if (S[i] != vals[(i + type) % 3]) errCount++;
      }
      best = min(best, errCount);
      for (int i = K; i < N; i++) {
        if (S[i] != vals[(i + type) % 3]) errCount++;
        if (S[i - K] != vals[(i + type - K) % 3]) errCount--;
        best = min(best, errCount);
      }
    }
    cout << best << endl;
  }
  return 0;
}
