#include <bits/stdc++.h>
using namespace std;
map<char, int> mp;
int main() {
  mp['R'] = 0;
  mp['G'] = 1;
  mp['B'] = 2;
  int n;
  cin >> n;
  while (n--) {
    int f[3][3] = {0};
    int len, k, ans = INT_MAX;
    cin >> len >> k;
    string s;
    cin >> s;
    for (int i = 1; i <= len; i++) {
      f[i % 3][mp[s[i - 1]]]++;
      if (i - k >= 1) f[(i - k) % 3][mp[s[(i - k - 1)]]]--;
      if (i >= k) {
        int a = f[0][0] + f[1][1] + f[2][2], b = f[0][2] + f[1][0] + f[2][1],
            c = f[0][1] + f[1][2] + f[2][0];
        ans = min(ans, min(min(k - a, k - b), k - c));
      }
    }
    cout << ans << endl;
  }
}
