#include <bits/stdc++.h>
using namespace std;
int cha(char c) {
  switch (c) {
    case 'R':
      return 0;
    case 'G':
      return 1;
    case 'B':
      return 2;
  }
  return -1;
}
int pre[200010];
int main() {
  int T;
  cin >> T;
  while (T--) {
    int n, k;
    cin >> n >> k;
    string s;
    cin >> s;
    int ans = 200010;
    for (int i = 0; i < 3; ++i) {
      pre[0] = (cha(s[0]) == i ? 1 : 0);
      int now = (i + 1) % 3;
      for (int j = 1; j <= n; ++j) {
        pre[j] = pre[j - 1] + (cha(s[j - 1]) == now ? 1 : 0);
        now = (now + 1) % 3;
      }
      for (int j = k; j <= n; ++j) {
        ans = min(ans, k - pre[j] + pre[j - k]);
      }
    }
    cout << ans << endl;
  }
  return 0;
}
