#include <bits/stdc++.h>
using namespace std;
char get(int i) {
  if (i % 3 == 0) {
    return 'R';
  }
  if (i % 3 == 1) {
    return 'G';
  }
  if (i % 3 == 2) {
    return 'B';
  }
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int q;
  cin >> q;
  while (q--) {
    int n, k;
    cin >> n >> k;
    string s;
    cin >> s;
    int counter1 = 0;
    int counter2 = 0;
    int counter3 = 0;
    int myMin = k;
    for (int l = 0; l < k; l++) {
      if (s[l] != get(l)) {
        counter1++;
      }
      if (s[l] != get(l + 1)) {
        counter2++;
      }
      if (s[l] != get(l + 2)) {
        counter3++;
      }
    }
    myMin = min(myMin, min(counter1, min(counter2, counter3)));
    for (int j = 0; j < n - k; j++) {
      if (get(j) != s[j]) {
        counter1--;
      }
      if (get(k + j) != s[j + k]) {
        counter1++;
      }
      if (get(j + 1) != s[j]) {
        counter2--;
      }
      if (get(k + j + 1) != s[j + k]) {
        counter2++;
      }
      if (get(j + 2) != s[j]) {
        counter3--;
      }
      if (get(k + j + 2) != s[j + k]) {
        counter3++;
      }
      myMin = min(myMin, min(counter1, min(counter2, counter3)));
    }
    cout << myMin << endl;
  }
  return 0;
}
