#include <bits/stdc++.h>
using namespace std;
int main() {
  long long int q;
  cin >> q;
  while (q--) {
    long long int n, k;
    cin >> n >> k;
    string s;
    cin >> s;
    string s1 = "RGB", s2 = "GBR", s3 = "BRG";
    long long int x1 = 0, x2 = 0, x3 = 0;
    long long int jj = 0;
    for (long long int i = 0; i < k; i++) {
      if (s[i] != s1[i % 3]) {
        x1++;
      }
      if (s[i] != s2[i % 3]) {
        x2++;
      }
      if (s[i] != s3[i % 3]) {
        x3++;
      }
    }
    long long int mn = min(x1, min(x2, x3));
    jj = 0;
    for (long long int i = 1; i <= s.length() - k; i++) {
      if (s[i - 1] != s1[(i - 1) % 3]) {
        if (x1 > 0) x1--;
      }
      if (s[i + k - 1] != s1[(i + k - 1) % 3]) x1++;
      if (s[i - 1] != s2[(i - 1) % 3]) {
        if (x2 > 0) x2--;
      }
      if (s[i + k - 1] != s2[(i + k - 1) % 3]) x2++;
      if (s[i - 1] != s3[(i - 1) % 3]) {
        if (x3 > 0) x3--;
      }
      if (s[(i + k - 1)] != s3[(i + k - 1) % 3]) x3++;
      mn = min(mn, min(x1, min(x2, x3)));
    }
    cout << mn << endl;
  }
}
