#include <bits/stdc++.h>
using namespace std;
long long n, k, res, q;
string s;
char a1[3] = {'R', 'G', 'B'};
char a2[3] = {'G', 'B', 'R'};
char a3[3] = {'B', 'R', 'G'};
signed main() {
  cin >> q;
  while (q--) {
    cin >> n >> k >> s;
    vector<long long> pr1(n, 0), pr2(n, 0), pr3(n, 0);
    long long ress1 = 0, ress2 = 0, ress3 = 0, res = 1e9;
    for (long long i = 0; i < n; ++i) {
      ress1 += s[i] != a1[i % 3];
      pr1[i] = ress1;
    }
    for (long long i = 0; i < n; ++i) {
      ress2 += s[i] != a2[i % 3];
      pr2[i] = ress2;
    }
    for (long long i = 0; i < n; ++i) {
      ress3 += s[i] != a3[i % 3];
      pr3[i] = ress3;
    }
    for (long long i = 0; i <= n - k; ++i) {
      long long c1 = pr1[i + k - 1] - (i ? pr1[i - 1] : 0);
      long long c2 = pr2[i + k - 1] - (i ? pr2[i - 1] : 0);
      long long c3 = pr3[i + k - 1] - (i ? pr3[i - 1] : 0);
      res = min(res, min(min(c1, c2), c3));
    }
    cout << res << '\n';
  }
}
