#include <bits/stdc++.h>
using namespace std;
char a[5] = {'R', 'G', 'B'};
char b[5] = {
    'B',
    'R',
    'G',
};
char c[5] = {'G', 'B', 'R'};
int cnt1[200005];
int cnt2[200005];
int cnt3[200005];
int main() {
  int t;
  int n, k;
  int ans;
  ios::sync_with_stdio(false);
  cin >> t;
  while (t--) {
    string s;
    ans = 1e9;
    cin >> n >> k;
    cin >> s;
    for (int i = 0; i < s.size(); i++) {
      int j = i % 3;
      if (s[i] != a[j])
        cnt1[i + 1] = cnt1[i] + 1;
      else
        cnt1[i + 1] = cnt1[i];
      if (s[i] != b[j])
        cnt2[i + 1] = cnt2[i] + 1;
      else
        cnt2[i + 1] = cnt2[i];
      if (s[i] != c[j])
        cnt3[i + 1] = cnt3[i] + 1;
      else
        cnt3[i + 1] = cnt3[i];
    }
    for (int i = 0; i <= s.size() - k; i++) {
      int l = i, r = i + k;
      if (cnt1[r] - cnt1[l] <= ans) ans = cnt1[r] - cnt1[l];
      if (cnt2[r] - cnt2[l] <= ans) ans = cnt2[r] - cnt2[l];
      if (cnt3[r] - cnt3[l] <= ans) ans = cnt3[r] - cnt3[l];
    }
    cout << ans << endl;
  }
  return 0;
}
