#include <bits/stdc++.h>
#pragma GCC optimize("Ofast", "unroll-loops")
using namespace std;
long long q, n, k, cnt, a[10000], ans;
string s;
int main() {
  ios::sync_with_stdio(0), cin.tie(0);
  cin >> q;
  while (q--) {
    cin >> n >> k >> s, ans = n;
    for (int i = 0; i < n; i++) {
      if (s[i] == 'R')
        a[i] = 0;
      else if (s[i] == 'G')
        a[i] = 1;
      else
        a[i] = 2;
    }
    for (int i = 0; i < 3; i++) {
      cnt = 0;
      for (int j = 0; j < s.size(); j++) {
        if (a[j] != (j + i) % 3) cnt++;
        if (j >= k && a[j - k] != (j + i - k + 300000) % 3) cnt--;
        if (j >= k - 1) ans = min(ans, cnt);
      }
    }
    cout << ans << '\n';
  }
}
