#include <bits/stdc++.h>
using namespace std;
int f() {
  int n, k;
  string s;
  cin >> n >> k >> s;
  vector<vector<int>> dp(n + 1, {0, 0, 0});
  map<char, int> ctoi;
  ctoi['R'] = 0;
  ctoi['G'] = 1;
  ctoi['B'] = 2;
  int ret = INT_MAX;
  for (int i = 1; i <= n; i++) {
    for (int c = 1; c <= 3; c++) {
      dp[i][c % 3] = dp[i - 1][c - 1] + (ctoi[s[i - 1]] != c % 3);
    }
    if (i >= k) {
      for (int c = 0; c <= 2; c++) {
        ret = min(ret, dp[i][c] - dp[i - k][(c + 3 - k % 3) % 3]);
      }
    }
  }
  return ret;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int T;
  cin >> T;
  while (T--) {
    cout << f() << '\n';
  }
  return 0;
}
