#include <bits/stdc++.h>
using namespace std;
void printvec(std::vector<long long> v) {
  long long n = v.size();
  for (long long i = 0; i < n; i++) cout << v[i] << " ";
  cout << endl;
}
long long mod = 1e9 + 7;
long long power(long long x, unsigned long long y) {
  long long temp;
  if (y == 0) return 1;
  temp = power(x, y / 2);
  if (y % 2 == 0)
    return temp * temp;
  else
    return x * temp * temp;
}
long long binarySearch(vector<long long> arr, long long l, long long r,
                       long long x) {
  if (r >= l) {
    long long mid = l + (r - l) / 2;
    if (arr[mid] == x) return mid;
    if (arr[mid] > x) return binarySearch(arr, l, mid - 1, x);
    return binarySearch(arr, mid + 1, r, x);
  }
  return -1;
}
long long gcd(long long n1, long long n2) {
  while (n1 != n2) {
    if (n1 > n2)
      n1 -= n2;
    else
      n2 -= n1;
  }
  return n1;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int T = 1;
  cin >> T;
  while (T--) {
    long long n, k;
    cin >> n >> k;
    string s;
    cin >> s;
    long long ans = mod;
    string t = "RGB";
    for (long long offset = 0; offset < 3; offset++) {
      std::vector<long long> diff(n);
      long long cur = 0;
      for (long long i = 0; i < n; i++) {
        diff[i] = (s[i] != (t[(i + offset) % 3]));
        cur += diff[i];
        if (i >= k) cur -= diff[i - k];
        if (i >= k - 1) ans = min(ans, cur);
      }
    }
    cout << ans << endl;
  }
}
