#include <bits/stdc++.h>
using namespace std;
long long mod(long long x) {
  return ((x % 1000000007 + 1000000007) % 1000000007);
}
long long add(long long a, long long b) { return mod(mod(a) + mod(b)); }
long long mul(long long a, long long b) { return mod(mod(a) * mod(b)); }
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  long long t;
  t = 1;
  cin >> t;
  while (t--) {
    int n, k;
    cin >> n >> k;
    string temp = "RGB";
    string s;
    cin >> s;
    int mainans = INT_MAX;
    int ans = 0;
    vector<bool> vis(n + 1);
    for (int i = 0; i < k; i++) {
      if (s[i] != temp[i % 3]) {
        vis[i] = 1;
        ans++;
      }
    }
    mainans = min(mainans, ans);
    for (int i = 1; i < n - k + 1; i++) {
      if (vis[i - 1]) {
        ans--;
      }
      if (s[i + k - 1] != temp[(i + k - 1) % 3]) {
        vis[i + k - 1] = 1;
        ans++;
      }
      mainans = min(ans, mainans);
    }
    for (int i = 0; i < n; i++) vis[i] = false;
    temp = "GBR";
    ans = 0;
    for (int i = 0; i < k; i++) {
      if (s[i] != temp[i % 3]) {
        vis[i] = 1;
        ans++;
      }
    }
    mainans = min(mainans, ans);
    for (int i = 1; i < n - k + 1; i++) {
      if (vis[i - 1]) {
        ans--;
      }
      if (s[i + k - 1] != temp[(i + k - 1) % 3]) {
        vis[i + k - 1] = 1;
        ans++;
      }
      mainans = min(ans, mainans);
    }
    for (int i = 0; i < n; i++) vis[i] = false;
    temp = "BRG";
    ans = 0;
    for (int i = 0; i < k; i++) {
      if (s[i] != temp[i % 3]) {
        vis[i] = 1;
        ans++;
      }
    }
    mainans = min(mainans, ans);
    for (int i = 1; i < n - k + 1; i++) {
      if (vis[i - 1]) {
        ans--;
      }
      if (s[i + k - 1] != temp[(i + k - 1) % 3]) {
        vis[i + k - 1] = 1;
        ans++;
      }
      mainans = min(ans, mainans);
    }
    cout << mainans << "\n";
  }
}
