#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  long long q, j;
  cin >> q;
  for (j = 0; j < q; j++) {
    long long n, k, i, ans1 = 999999999999;
    cin >> n >> k;
    string s;
    cin >> s;
    vector<long long> v;
    for (i = 0; i < n; i++) {
      if (s[i] == 'R')
        v.push_back(0);
      else if (s[i] == 'G')
        v.push_back(1);
      else if (s[i] == 'B')
        v.push_back(2);
    }
    vector<long long> aa, bb, cc, ans(n + 5, 0);
    for (i = 0; i < n; i++) aa.push_back(i % 3);
    for (i = 0; i < n; i++) bb.push_back((i + 1) % 3);
    for (i = 0; i < n; i++) cc.push_back((i + 2) % 3);
    for (i = 0; i < n; i++) {
      if (v[i] == aa[i]) ans[i + 1] = 1;
      ans[i + 1] += ans[i];
    }
    for (i = k; i <= n; i++) {
      if (ans[i] - ans[i - k] > k)
        ans1 = 0;
      else
        ans1 = min(ans1, k - ans[i] + ans[i - k]);
    }
    for (i = 0; i < n + 5; i++) ans[i] = 0;
    for (i = 0; i < n; i++) {
      if (v[i] == bb[i]) ans[i + 1] = 1;
      ans[i + 1] += ans[i];
    }
    for (i = k; i <= n; i++) {
      if (ans[i] - ans[i - k] > k)
        ans1 = 0;
      else
        ans1 = min(ans1, k - ans[i] + ans[i - k]);
    }
    for (i = 0; i < n + 5; i++) ans[i] = 0;
    for (i = 0; i < n; i++) {
      if (v[i] == cc[i]) ans[i + 1] = 1;
      ans[i + 1] += ans[i];
    }
    for (i = k; i <= n; i++) {
      if (ans[i] - ans[i - k] > k)
        ans1 = 0;
      else
        ans1 = min(ans1, k - ans[i] + ans[i - k]);
    }
    cout << ans1 << endl;
  }
  return 0;
}
