#include <bits/stdc++.h>
using namespace std;
int q, n, k, dp[3][200005];
string s, a[3] = {"RGB", "GBR", "BRG"};
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> q;
  while (q--) {
    cin >> n >> k >> s;
    for (int i = 0; i < 3; i++) {
      dp[i][0] = 0;
      for (int j = 0; j < n; j++) {
        dp[i][j + 1] = dp[i][j] + (s[j] == a[i][j % 3] ? 0 : 1);
      }
    }
    int ans = 1e9;
    for (int i = 0; i < 3; i++) {
      for (int j = k; j <= n; j++) {
        ans = min(ans, dp[i][j] - dp[i][j - k]);
      }
    }
    cout << ans << '\n';
  }
  return 0;
}
