#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 7;
string x[3] = {"RGB", "GBR", "BRG"};
int y[3][N];
int main() {
  int t;
  cin >> t;
  while (t--) {
    int n, k;
    cin >> n >> k;
    string s;
    cin >> s;
    int ans = 1000000;
    for (int g = 0; g < 3; g++) {
      int cnt = 0;
      for (int i = 0; i < n; i++) {
        y[g][i] = (s[i] != x[g][i % 3]);
      }
      for (int i = 0; i < k; i++) cnt += y[g][i];
      ans = min(ans, cnt);
      for (int i = k; i < n; i++) {
        cnt += y[g][i];
        cnt -= y[g][i - k];
        ans = min(ans, cnt);
      }
    }
    cout << ans << '\n';
  }
  return 0;
}
