#include <bits/stdc++.h>
using namespace std;
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;
    int s[n];
    char c;
    for (int i = 0; i < n; i++) {
      cin >> c;
      if (c == 'R')
        s[i] = 0;
      else if (c == 'G')
        s[i] = 1;
      else
        s[i] = 2;
    }
    int counter1 = 0;
    int counter2 = 0;
    int counter3 = 0;
    int myMin = k;
    for (int l = 0; l < k; l++) {
      if (s[l] != l % 3) {
        counter1++;
      }
      if (s[l] != (l + 1) % 3) {
        counter2++;
      }
      if (s[l] != (l + 2) % 3) {
        counter3++;
      }
    }
    myMin = min(myMin, min(counter1, min(counter2, counter3)));
    for (int j = 0; j < n - k; j++) {
      if (j % 3 != s[j]) {
        counter1--;
      }
      if ((k + j) % 3 != s[j + k]) {
        counter1++;
      }
      if ((j + 1) % 3 != s[j]) {
        counter2--;
      }
      if ((k + j + 1) % 3 != s[j + k]) {
        counter2++;
      }
      if ((j + 2) % 3 != s[j]) {
        counter3--;
      }
      if ((k + j + 2) % 3 != s[j + k]) {
        counter3++;
      }
      myMin = min(myMin, min(counter1, min(counter2, counter3)));
    }
    cout << myMin << endl;
  }
  return 0;
}
