#include <bits/stdc++.h>
using namespace std;
map<char, int> ind;
vector<vector<int>> per;
int g(int st, int ed, int k) {
  int ans = (ed - st + 1) / 3;
  for (int i = ed; i > ed - (ed - st + 1) % 3; i--) {
    if (i % 3 == k) ans++;
  }
  return ans;
}
void f() {
  int n, k;
  cin >> n >> k;
  string s;
  cin >> s;
  int a[n][3][3];
  memset(a, 0, sizeof a);
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < 3; j++) {
      for (int kk = 0; kk < 3; kk++) {
        a[i][j][kk] = (i ? a[i - 1][j][kk] : 0);
      }
    }
    a[i][i % 3][ind[s[i]]]++;
  }
  int ans = n;
  for (int i = 0; i + k - 1 < n; i++) {
    for (int j = 0; j < 3; j++) {
      int t = 0;
      int kk = i % 3;
      int c = 3;
      int fir = 0;
      while (c--) {
        int same =
            (i ? a[i + k - 1][kk][per[j][fir]] - a[i - 1][kk][per[j][fir]]
               : a[i + k - 1][kk][per[j][fir]]);
        t += g(i, i + k - 1, kk) - same;
        fir++;
        kk = (kk + 1) % 3;
      }
      ans = min(ans, t);
    }
  }
  cout << ans << '\n';
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  ind['R'] = 0, ind['G'] = 1, ind['B'] = 2;
  per = {{0, 1, 2}, {1, 2, 0}, {2, 0, 1}};
  long long int q;
  cin >> q;
  while (q--) {
    f();
  }
}
