#include <bits/stdc++.h>
using namespace std;
template <typename _T>
inline void _DBG(const char *s, _T x) {
  cerr << s << " = " << x << "\n";
}
template <typename _T, typename... args>
void _DBG(const char *s, _T x, args... a) {
  while (*s != ',') cerr << *s++;
  cerr << " = " << x << ',';
  _DBG(s + 1, a...);
}
const int N = 2e5 + 7;
int n, k, T[N], pre[N][3][3];
char s[N], mp[256];
int ask(int l, int r, int v, int m3) {
  return pre[r][v][m3] - pre[l - 1][v][m3];
}
int calc(int i, int a, int b, int c) {
  int j = i + k - 1, ans = k;
  ans -= ask(i, j, a, i % 3);
  ans -= ask(i, j, b, (i + 1) % 3);
  ans -= ask(i, j, c, (i + 2) % 3);
  return ans;
}
void solve() {
  cin >> n >> k >> (s + 1);
  for (int i = 1; i <= n; i++) T[i] = mp[s[i]];
  for (int i = 0; i <= n; i++)
    for (int j = 0; j < 3; j++)
      for (int l = 0; l < 3; l++) pre[i][j][l] = 0;
  for (int i = 1; i <= n; i++) {
    for (int j = 0; j < 3; j++)
      for (int l = 0; l < 3; l++) pre[i][j][l] = pre[i - 1][j][l];
    pre[i][T[i]][i % 3]++;
  }
  int mn = N;
  for (int i = 1; i + k - 1 <= n; i++) {
    mn = min(mn, calc(i, 0, 1, 2));
    mn = min(mn, calc(i, 1, 2, 0));
    mn = min(mn, calc(i, 2, 0, 1));
  }
  cout << mn << "\n";
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  mp['R'] = 0;
  mp['G'] = 1;
  mp['B'] = 2;
  int q;
  cin >> q;
  while (q--) {
    solve();
  }
  return 0;
}
