#include <bits/stdc++.h>
long long int MOD = (long long int)(1e9 + 7);
using namespace std;
long long int add(long long a, long long b) {
  return ((a % MOD) + (b % MOD)) % MOD;
}
long long int mul(long long a, long long b) {
  return ((a % MOD) * (b % MOD)) % MOD;
}
long long int sub(long long a, long long b) {
  long long int ans = ((a % MOD - b % MOD)) % MOD;
  if (ans < 0) ans += MOD;
  return ans;
}
long long int modInverse(long long int a) {
  long long int m = MOD, m0 = m, y = 0, x = 1;
  if (m == 1) return 0;
  while (a > 1) {
    long long int q = a / m, t = m;
    m = a % m, a = t, t = y;
    y = x - q * y, x = t;
  }
  if (x < 0) x += m0;
  return x;
}
long long int gcd(long long int a, long long int b) {
  if (b == 0) return a;
  return gcd(b, a % b);
}
void MAIN(long long int i, long long int t);
int get() {
  long long int t;
  cin >> t;
  ;
  return t;
}
int log(string name) {
  cout << name << endl;
  return 0;
}
void test(int t) {
  for (long long int i = 0; i < t; i++) {
    MAIN(i, t);
    cout << endl;
  }
}
long long int power(long long int x, long long int y) {
  long long int res = 1;
  x = x % MOD;
  if (x == 0) return 0;
  while (y > 0) {
    if (y & 1) res = (res * x) % MOD;
    y = y >> 1;
    x = (x * x) % MOD;
  }
  return res;
}
long long int popcount(long long int x) { return __builtin_popcount(x); }
long long int lcm(long long int a, long long int b) {
  return (a * b) / gcd(a, b);
}
string s;
long long int gt(string ro, long long int k) {
  vector<long long int> prefix(s.size());
  for (int j = 0; j < 3; j++)
    for (int i = j; i < s.size(); i += 3) {
      prefix[i] = (ro[j] != s[i]);
    }
  for (int i = 1; i < prefix.size(); i++) {
    prefix[i] += prefix[i - 1];
  }
  long long int ans = 1e17;
  for (int i = 0; i <= prefix.size() - k; i++) {
    long long int last = 0;
    if (i - 1 >= 0) last = prefix[i - 1];
    long long int temp = prefix[i + k - 1] - last;
    ans = min(ans, temp);
  }
  return ans;
}
long long int k, n;
void MAIN(long long int current_test_case, long long int total_test_cases) {
  s.resize(0);
  cin >> n >> k >> s;
  vector<long long int> v;
  v.push_back(gt((string) "RGB", k));
  v.push_back(gt((string) "GBR", k));
  v.push_back(gt((string) "BRG", k));
  sort((v).begin(), (v).end());
  cout << v[0];
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  test(get());
  return 0;
}
