#include <bits/stdc++.h>
using namespace std;
const int inf = numeric_limits<int>::max();
const long long int linf = numeric_limits<long long int>::max();
const long long int MOD = (long long int)1e9 + 7;
const long long int mod = 998244353;
long long int fast_mod(long long int abcd, int expp) {
  long long int res = 1;
  while (expp > 0) {
    if (expp % 2 == 1) res = (res * abcd) % MOD;
    abcd = (abcd * abcd) % MOD;
    expp /= 2;
  }
  return res % MOD;
}
long long int gcd(long long int a, long long int b) {
  long long int temppp = max(a, b);
  b = min(a, b);
  a = temppp;
  if (b == 0) return a;
  return gcd(b, a % b);
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int q, k, n;
  string s;
  cin >> q;
  int dp1[100001];
  int dp2[100001];
  int dp3[100001];
  while (q--) {
    cin >> n >> k;
    string s;
    cin >> s;
    memset(dp1, 0, sizeof dp1);
    memset(dp2, 0, sizeof dp2);
    memset(dp3, 0, sizeof dp3);
    if (s[0] == 'R')
      dp2[0] = dp3[0] = 1;
    else if (s[0] == 'G')
      dp1[0] = dp3[0] = 1;
    else if (s[0] == 'B')
      dp1[0] = dp2[0] = 1;
    for (int i = 1; i < n; i++) {
      if (i % 3 == 0) {
        if (s[i] == 'R') {
          dp1[i] = dp1[i - 1];
          dp2[i] = 1 + dp2[i - 1];
          dp3[i] = 1 + dp3[i - 1];
        } else if (s[i] == 'G') {
          dp1[i] = 1 + dp1[i - 1];
          dp2[i] = dp2[i - 1];
          dp3[i] = 1 + dp3[i - 1];
        } else if (s[i] == 'B') {
          dp1[i] = 1 + dp1[i - 1];
          dp2[i] = 1 + dp2[i - 1];
          dp3[i] = dp3[i - 1];
        }
      } else if (i % 3 == 1) {
        if (s[i] == 'G') {
          dp1[i] = dp1[i - 1];
          dp2[i] = 1 + dp2[i - 1];
          dp3[i] = 1 + dp3[i - 1];
        } else if (s[i] == 'B') {
          dp1[i] = 1 + dp1[i - 1];
          dp2[i] = dp2[i - 1];
          dp3[i] = 1 + dp3[i - 1];
        } else if (s[i] == 'R') {
          dp1[i] = 1 + dp1[i - 1];
          dp2[i] = 1 + dp2[i - 1];
          dp3[i] = dp3[i - 1];
        }
      } else {
        if (s[i] == 'B') {
          dp1[i] = dp1[i - 1];
          dp2[i] = 1 + dp2[i - 1];
          dp3[i] = 1 + dp3[i - 1];
        } else if (s[i] == 'R') {
          dp1[i] = 1 + dp1[i - 1];
          dp2[i] = dp2[i - 1];
          dp3[i] = 1 + dp3[i - 1];
        } else if (s[i] == 'G') {
          dp1[i] = 1 + dp1[i - 1];
          dp2[i] = 1 + dp2[i - 1];
          dp3[i] = dp3[i - 1];
        }
      }
    }
    long long int ans = dp1[k - 1];
    if (dp2[k - 1] < ans) ans = dp2[k - 1];
    if (dp3[k - 1] < ans) ans = dp3[k - 1];
    for (int i = 0; i < n - k; i++) {
      if (dp1[i + k] - dp1[i] < ans) ans = dp1[i + k] - dp1[i];
      if (dp2[i + k] - dp2[i] < ans) ans = dp2[i + k] - dp2[i];
      if (dp3[i + k] - dp3[i] < ans) ans = dp3[i + k] - dp3[i];
    }
    cout << ans << "\n";
  }
  return 0;
}
