#include <bits/stdc++.h>
using namespace std;
inline long long max(long long a, long long b);
inline long long min(long long a, long long b);
long long max(long long a, long long b) { return (a > b ? a : b); }
long long min(long long a, long long b) { return (a < b ? a : b); }
long long kuai(long long a, long long b) {
  long long ans = 1;
  while (b) {
    if (b & 1) {
      ans = ans * a;
    }
    a = a * a;
    b = b / 2;
  }
  return ans;
}
long long gcd(long long a, long long b) { return b == 0 ? a : gcd(b, a % b); }
long long lcm(long long a, long long b) { return a * b / gcd(a, b); }
const long long maxn = 2e5 + 10;
const long long N = 1e5 + 10;
char a[maxn];
long long val[maxn];
long long sum[maxn];
signed main() {
  long long t;
  cin >> t;
  while (t--) {
    long long n, k;
    cin >> n >> k;
    string s;
    cin >> s;
    for (long long i = 0; i < n + 3; i += 3) {
      a[i] = 'R';
      a[i + 1] = 'G';
      a[i + 2] = 'B';
    }
    long long ans = 2e5 + 10;
    for (long long i = 0; i < 3; i++) {
      for (long long j = 0; j < n; j++) {
        if (a[j + i] == s[j])
          val[j] = 0;
        else
          val[j] = 1;
      }
      for (long long j = 0; j < n; j++) {
        sum[j] = sum[j - 1] + val[j];
      }
      for (long long j = k - 1; j < n; j++) {
        ans = min(ans, sum[j] - sum[j - k]);
      }
    }
    cout << ans << "\n";
  }
  return 0;
}
