#include <bits/stdc++.h>
using namespace std;
const long long mod = 1e9 + 7;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  long long q;
  cin >> q;
  while (q-- > 0) {
    long long n, k;
    cin >> n >> k;
    string s;
    cin >> s;
    long long cnt1[n], cnt2[n], cnt3[n];
    for (long long i = 0; i < n; i++) {
      cnt1[i] = 0;
      cnt2[i] = 0;
      cnt3[i] = 0;
    }
    for (long long i = 0; i < n; i++) {
      if (i)
        cnt1[i] = cnt1[i - 1], cnt2[i] = cnt2[i - 1], cnt3[i] = cnt3[i - 1];
      if (i % 3 == 0) {
        if (s[i] != 'R') cnt1[i]++;
        if (s[i] != 'G') cnt2[i]++;
        if (s[i] != 'B') cnt3[i]++;
      } else if (i % 3 == 1) {
        if (s[i] != 'G') cnt1[i]++;
        if (s[i] != 'B') cnt2[i]++;
        if (s[i] != 'R') cnt3[i]++;
      } else {
        if (s[i] != 'B') cnt1[i]++;
        if (s[i] != 'R') cnt2[i]++;
        if (s[i] != 'G') cnt3[i]++;
      }
    }
    long long mi = min(cnt1[k - 1], min(cnt2[k - 1], cnt3[k - 1]));
    for (long long i = 0; i + k < n; i++) {
      mi = min(min(mi, cnt1[i + k] - cnt1[i]),
               min(cnt2[i + k] - cnt2[i], cnt3[i + k] - cnt3[i]));
    }
    cout << mi << "\n";
  }
}
