#include <bits/stdc++.h>
using namespace std;
long double EPS = 1e-9;
long long cel(long long a, long long b) { return ((a - 1) / b + 1); }
long long gcd(long long a, long long b) {
  if (a < b) swap(a, b);
  return (b == 0) ? a : gcd(b, a % b);
}
long long MIN(long long a, int b) {
  long long ans;
  (a >= b) ? ans = b : ans = a;
  return ans;
}
long long MAX(long long a, int b) {
  long long ans;
  (a >= b) ? ans = a : ans = b;
  return ans;
}
long long lcm(long long a, long long b) { return (a * b) / gcd(a, b); }
long long po(long long x, long long y) {
  long long ans = 1;
  while (y) {
    if (y & 1) {
      ans = (ans * x) % 1000000007;
    }
    y >>= 1;
    x = (x * x) % 1000000007;
  }
  return ans;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  long long n, t, x, y, z, m, k;
  cin >> t;
  while (t--) {
    cin >> n >> k;
    int dp[n][4];
    string s;
    cin >> s;
    string R = "RGB", G = "GBR", B = "BRG";
    char r, g, b;
    r = (R[(k - 1) % 3]);
    g = (G[(k - 1) % 3]);
    b = (B[(k - 1) % 3]);
    x = y = z = 0;
    for (int i = 0; i < k; i++) {
      if (R[i % 3] != s[i]) {
        x++;
      }
      if (G[i % 3] != s[i]) {
        y++;
      }
      if (B[i % 3] != s[i]) {
        z++;
      }
    }
    dp[k - 1][1] = x;
    dp[k - 1][2] = y;
    dp[k - 1][3] = z;
    int ans = min(x, min(y, z));
    for (int i = k; i < n; i++) {
      dp[i][1] = dp[i - 1][3];
      if (s[i] != r) dp[i][1]++;
      if (s[i - k] != 'B') dp[i][1]--;
      dp[i][2] = dp[i - 1][1];
      if (s[i] != g) dp[i][2]++;
      if (s[i - k] != 'R') dp[i][2]--;
      dp[i][3] = dp[i - 1][2];
      if (s[i] != b) dp[i][3]++;
      if (s[i - k] != 'G') dp[i][3]--;
      ans = min(ans, min(dp[i][1], min(dp[i][2], dp[i][3])));
    }
    cout << ans << "\n";
  }
  return 0;
}
