#include <bits/stdc++.h>
using namespace std;
int const maxn = int(2 * 1e5 + 5);
int dp1[maxn], dp2[maxn], dp3[maxn], n, k, t;
string second;
char chuyen(char c) {
  if (c == 'R') return 'G';
  if (c == 'G') return 'B';
  if (c == 'B') return 'R';
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cin >> t;
  while (t--) {
    cin >> n >> k >> second;
    second = " " + second;
    for (int i = 0; i <= second.length(); i++) {
      dp1[i] = dp2[i] = dp3[i] = 0;
    }
    char c = 'R';
    for (int i = 1; i <= second.length() - 1; i++) {
      if (second[i] != c)
        dp1[i] = dp1[i - 1] + 1;
      else
        dp1[i] = dp1[i - 1];
      c = chuyen(c);
    }
    c = 'G';
    for (int i = 1; i <= second.length() - 1; i++) {
      if (second[i] != c)
        dp2[i] = dp2[i - 1] + 1;
      else
        dp2[i] = dp2[i - 1];
      c = chuyen(c);
    }
    c = 'B';
    for (int i = 1; i <= second.length() - 1; i++) {
      if (second[i] != c)
        dp3[i] = dp3[i - 1] + 1;
      else
        dp3[i] = dp3[i - 1];
      c = chuyen(c);
    }
    int res = int(1e9 + 5);
    for (int i = 1; i <= second.length() - k; i++) {
      res = min(res, dp1[i + k - 1] - dp1[i - 1]);
      res = min(res, dp2[i + k - 1] - dp2[i - 1]);
      res = min(res, dp3[i + k - 1] - dp3[i - 1]);
    }
    cout << res << '\n';
  }
  return 0;
}
