#include <bits/stdc++.h>
using namespace std;
inline void fileio(const char* in, const char* out) {
  freopen(in, "r", stdin);
  freopen(out, "w", stdout);
}
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> VI;
typedef vector<bool> VB;
typedef vector<ll> VL;
const char nl = '\n';
const int INF = 0x3f3f3f3f;
const ll LINF = 0x3f3f3f3f3f3f3f3fll;
const ld EPS = 1e-9, PI = acos(-1);
int ord(char c) {
  switch (c) {
    case 'R':
      return 0;
    case 'G':
      return 1;
    case 'B':
      return 2;
  }
  throw "poops";
}
char chr(int i) {
  static char t[] = {'R', 'G', 'B'};
  return t[i];
}
void deal() {
  int n, k;
  cin >> n >> k;
  string s;
  cin >> s;
  vector<VI> dp(n - k + 1, VI(3, 0));
  int best = k + 10;
  for (int j = 0; j < (3); ++j) {
    dp[n - k][j] = 0;
    int cnt = j;
    for (int i = n - k; i < n; ++i, ++cnt) {
      if (s[i] != chr(cnt % 3)) dp[n - k][j]++;
    }
    best = min(best, dp[n - k][j]);
  }
  for (int i = n - k; i > 0; --i) {
    for (int j = 0; j < (3); ++j) {
      dp[i - 1][j] = dp[i][(j + 1) % 3];
      if (s[i + k - 1] != chr((j + k) % 3)) {
        dp[i - 1][j]--;
      }
      if (s[i - 1] != chr(j)) {
        dp[i - 1][j]++;
      }
      best = min(best, dp[i - 1][j]);
    }
  }
  cout << best << nl;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int q;
  cin >> q;
  for (int i = 0; i < (q); ++i) deal();
  cerr << "Time: " << (ld)clock() / CLOCKS_PER_SEC << nl;
  return 0;
}
