#include <bits/stdc++.h>
using namespace std;
long long mod = 1e9 + 7;
int rx[4] = {0, 0, 1, -1};
int ry[4] = {-1, 1, 0, 0};
int rr[4] = {-1, -1, 1, 1};
int cr[4] = {-1, 1, -1, 1};
long long inf = 1e18 + 1;
long long ct = 1e5;
long long max(long long a, long long b) {
  if (a >= b) {
    return a;
  }
  return b;
}
long long min(long long a, long long b) {
  if (a <= b) {
    return a;
  }
  return b;
}
int comp(string a, string b) {
  int c = 0;
  for (int i = 0; i < a.length(); i++) {
    c += (a[i] == b[i]);
  }
  return c;
}
int main() {
  string p = "RGB", qq = "";
  for (int i = 0; i < ct; i++) {
    qq += p;
  }
  int q, n, k;
  string s;
  cin >> q;
  while (q--) {
    cin >> n >> k;
    cin >> s;
    vector<vector<int> > pre(3, vector<int>(n, 0));
    for (int i = 0; i < 3; i++) {
      for (int j = i; j < i + n; j++) {
        pre[i][j - i] = (qq[j] != s[j - i]);
      }
    }
    int ans = k + 1;
    for (int i = 0; i < 3; i++) {
      int curSum = 0;
      for (int j = 0; j < k; j++) {
        curSum += pre[i][j];
      }
      ans = min(curSum, ans);
      for (int j = 1; j <= n - k; j++) {
        curSum -= pre[i][j - 1];
        curSum += pre[i][j + k - 1];
        ans = min(curSum, ans);
      }
    }
    cout << ans << endl;
  }
  return 0;
}
