#include <bits/stdc++.h>
using namespace std;
using Graph = vector<vector<int>>;
int main() {
  int t;
  cin >> t;
  for (long long _ = 0; _ < (t); ++_) {
    int n, k;
    cin >> n >> k;
    string s;
    cin >> s;
    string jud1 = "RGB";
    string jud2 = "GBR";
    string jud3 = "BRG";
    vector<bool> JUD1(n, false);
    vector<bool> JUD2(n, false);
    vector<bool> JUD3(n, false);
    long long ans = 1002003004;
    long long cnt1 = k;
    for (int i = 0; i <= k - 1; i++) {
      if (s[i] == jud1[i % 3]) {
        cnt1--;
        JUD1[i] = true;
      }
    }
    ans = min(ans, cnt1);
    for (int i = k; i < n; i++) {
      if (s[i] == jud1[i % 3]) {
        cnt1--;
        JUD1[i] = true;
      }
      if (JUD1[i - k]) cnt1++;
      ans = min(ans, cnt1);
    }
    long long cnt2 = k;
    for (int i = 0; i <= k - 1; i++) {
      if (s[i] == jud2[i % 3]) {
        cnt2--;
        JUD2[i] = true;
      }
    }
    ans = min(ans, cnt2);
    for (int i = k; i < n; i++) {
      if (s[i] == jud2[i % 3]) {
        cnt2--;
        JUD2[i] = true;
      }
      if (JUD2[i - k]) cnt2++;
      ans = min(ans, cnt2);
    }
    long long cnt3 = k;
    for (int i = 0; i <= k - 1; i++) {
      if (s[i] == jud3[i % 3]) {
        cnt3--;
        JUD3[i] = true;
      }
    }
    ans = min(ans, cnt3);
    for (int i = k; i < n; i++) {
      if (s[i] == jud3[i % 3]) {
        cnt3--;
        JUD3[i] = true;
      }
      if (JUD3[i - k]) cnt3++;
      ans = min(ans, cnt3);
    }
    cout << ans << endl;
  }
}
