#include <bits/stdc++.h>
using namespace std;
char c[3] = {'R', 'G', 'B'};
signed main() {
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  long long TEST_CASE;
  cin >> TEST_CASE;
  for (long long TEST_NUM = 1; TEST_NUM <= TEST_CASE; TEST_NUM++) {
    long long n, k;
    cin >> n >> k;
    string s;
    cin >> s;
    long long so = ((1LL << 31) - 1);
    for (long long st = 0; st < 3; st++) {
      long long x = 0, y = 0, sum = 0, i = st;
      vector<long long> v(n);
      for (long long j = 0; j < n; j++) {
        v[j] = s[j] != c[i];
        i = (i + 1) % 3;
      }
      for (; y < k; y++) {
        sum += v[y];
      }
      y--;
      so = min(so, sum);
      while (true) {
        if (!(++y < n)) break;
        sum += v[y];
        sum -= v[x++];
        so = min(so, sum);
      }
    }
    cout << so << '\n';
  }
}
