#include <bits/stdc++.h>
using namespace std;
const long long maxn = 1e6;
long long t, k, n, cnt, ans, num[3][maxn];
string mub = "RGB";
char str[maxn];
map<char, long long> mp;
int main() {
  cin >> t;
  while (t--) {
    cin >> n >> k >> (str + 1);
    ans = 0x3f3f3f3f;
    for (int h = 0; h < 3; h++) {
      for (int i = 1; i <= n; i++)
        num[h][i] = num[h][i - 1] + (str[i] == mub[(h + i - 1) % 3]);
    }
    for (int h = 0; h < 3; h++) {
      for (int i = k; i <= n; i++)
        ans = min(ans, k - (num[h][i] - num[h][i - k]));
    }
    cout << ans << endl;
  }
  return 0;
}
