#include <bits/stdc++.h>
using namespace std;
const int N = 3e3 + 5, inf = 2e9, mod = 1e9 + 7;
vector<int> prime_Numbers;
bool primes[1000005];
double dot(complex<double> a, complex<double> b) {
  return (conj(a) * b).real();
}
double cross(complex<double> a, complex<double> b) {
  return (conj(a) * b).imag();
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int t;
  cin >> t;
  while (t--) {
    int n, k;
    cin >> n >> k;
    long long acc1[n + 2], acc2[n + 2], acc3[n + 2];
    memset(acc3, 0, sizeof acc3);
    string s, t = "RGB";
    cin >> s;
    for (int j = 0; j < 3; j++) {
      for (int i = 0; i < s.size(); i++) {
        if (s[i] != t[(j + i) % 3]) {
          if (j == 0) {
            if (i == 0)
              acc1[i] = 1;
            else
              acc1[i] = acc1[i - 1] + 1;
          }
          if (j == 1) {
            if (i == 0)
              acc2[i] = 1;
            else
              acc2[i] = acc2[i - 1] + 1;
          }
          if (j == 2) {
            if (i == 0)
              acc3[i] = 1;
            else
              acc3[i] = acc3[i - 1] + 1;
          }
        } else {
          if (j == 0) {
            if (i == 0)
              acc1[i] = 0;
            else
              acc1[i] = acc1[i - 1];
          }
          if (j == 1) {
            if (i == 0)
              acc2[i] = 0;
            else
              acc2[i] = acc2[i - 1];
          }
          if (j == 2) {
            if (i == 0)
              acc3[i] = 0;
            else
              acc3[i] = acc3[i - 1];
          }
        }
      }
    }
    long long ans = 1e9;
    acc1[-1] = 0;
    acc2[-1] = 0;
    acc3[-1] = 0;
    for (int i = 0; i <= n - k; i++) {
      ans = min(acc1[i + k - 1] - acc1[i - 1],
                min(acc2[i + k - 1] - acc2[i - 1],
                    min(acc3[i + k - 1] - acc3[i - 1], ans)));
    }
    cout << ans << endl;
  }
  return 0;
}
