#include <bits/stdc++.h>
using namespace std;
char getC(int i) {
  if (i == 0) return 'R';
  if (i == 1) return 'G';
  return 'B';
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int q;
  cin >> q;
  while (q--) {
    string second;
    int n, k;
    cin >> n >> k >> second;
    int cnt[3];
    cnt[0] = cnt[1] = cnt[2] = 0;
    for (int i = 0; i < (k); i++) {
      if (getC(i % 3) != second[i]) cnt[0]++;
      if (getC((i + 1) % 3) != second[i]) cnt[1]++;
      if (getC((i + 2) % 3) != second[i]) cnt[2]++;
    }
    int ans = min(cnt[0], min(cnt[1], cnt[2]));
    for (int i = k; i < (n); i++) {
      if (getC((i - k) % 3) != second[i - k]) cnt[0]--;
      if (getC((i - k + 1) % 3) != second[i - k]) cnt[1]--;
      if (getC((i - k + 2) % 3) != second[i - k]) cnt[2]--;
      if (getC(i % 3) != second[i]) cnt[0]++;
      if (getC((i + 1) % 3) != second[i]) cnt[1]++;
      if (getC((i + 2) % 3) != second[i]) cnt[2]++;
      ans = min(ans, min(cnt[0], min(cnt[1], cnt[2])));
    }
    cout << ans << endl;
  }
  cin >> q;
  return 0;
}
