#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  long long int t = 1;
  cin >> t;
  string s1 = "";
  for (long long int i = 0; i < 80000; i++) {
    s1.push_back('R');
    s1.push_back('G');
    s1.push_back('B');
  }
  while (t--) {
    long long int n, k, i, j, x, ans = 500000;
    cin >> n >> k;
    string s;
    cin >> s;
    long long int pref1[n + 10], pref2[n + 10], pref3[n + 10];
    memset(pref1, 0, sizeof(pref1));
    memset(pref2, 0, sizeof(pref2));
    memset(pref3, 0, sizeof(pref3));
    j = 0;
    for (i = 0; i < n; i++, j++) {
      if (s[i] != s1[j])
        pref1[i + 1] = pref1[i] + 1;
      else
        pref1[i + 1] = pref1[i];
    }
    j = 1;
    for (i = 0; i < n; i++, j++) {
      if (s[i] != s1[j])
        pref2[i + 1] = pref2[i] + 1;
      else
        pref2[i + 1] = pref2[i];
    }
    j = 2;
    for (i = 0; i < n; i++, j++) {
      if (s[i] != s1[j])
        pref3[i + 1] = pref3[i] + 1;
      else
        pref3[i + 1] = pref3[i];
    }
    for (i = 0; i < n - k + 1; i++) {
      ans = min(ans, pref1[i + k] - pref1[i]);
      ans = min(ans, pref2[i + k] - pref2[i]);
      ans = min(ans, pref3[i + k] - pref3[i]);
    }
    cout << ans << "\n";
  }
  return 0;
}
