#include <bits/stdc++.h>
using namespace std;
int solve(int n, int k, string s, string a) {
  int ans;
  int c = 0;
  for (int i = 0; i < k; i++) {
    if (s[i] != a[i % 3]) c++;
  }
  ans = c;
  for (int i = k; i <= n - 1; i++) {
    if (s[i] != a[i % 3]) c++;
    if (s[i - k] != a[(i - k) % 3]) c--;
    ans = min(ans, c);
  }
  return ans;
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  ;
  int t;
  cin >> t;
  while (t--) {
    int n, k;
    cin >> n >> k;
    string s;
    cin >> s;
    cout << min(min(solve(n, k, s, "RGB"), solve(n, k, s, "GBR")),
                solve(n, k, s, "BRG"))
         << endl;
  }
}
