#include <bits/stdc++.h>
using namespace std;
const int MAX = 2e5 + 50;
int sum[MAX];
int main() {
  int q, k, n;
  string s = "RGB", rgb = "";
  for (int i = 0; i < MAX; ++i) {
    rgb += s[i % 3];
  }
  cin >> q;
  while (q--) {
    cin >> n >> k >> s;
    int ans = MAX;
    for (int i = 0; s[i]; ++i) {
      if (s[i] != rgb[i])
        sum[i] = i == 0 ? 1 : 1 + sum[i - 1];
      else
        sum[i] = i == 0 ? 0 : sum[i - 1];
      if (i + 1 >= k)
        ans = i == k - 1 ? min(ans, sum[i]) : min(ans, sum[i] - sum[i - k]);
    }
    for (int i = 0; s[i]; ++i) {
      if (s[i] != rgb[i + 1])
        sum[i] = i == 0 ? 1 : 1 + sum[i - 1];
      else
        sum[i] = i == 0 ? 0 : sum[i - 1];
      if (i + 1 >= k)
        ans = i == k - 1 ? min(ans, sum[i]) : min(ans, sum[i] - sum[i - k]);
    }
    for (int i = 0; s[i]; ++i) {
      if (s[i] != rgb[i + 2])
        sum[i] = i == 0 ? 1 : 1 + sum[i - 1];
      else
        sum[i] = i == 0 ? 0 : sum[i - 1];
      if (i + 1 >= k)
        ans = i == k - 1 ? min(ans, sum[i]) : min(ans, sum[i] - sum[i - k]);
    }
    cout << ans << endl;
  }
}
