#include <bits/stdc++.h>
using namespace std;
long long T, n, k, ans;
string a, b, c, s;
long long arr_a[200000 + 5], arr_b[200000 + 5], arr_c[200000 + 5];
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  for (long long i = 0; i < 70000; i++) {
    a += "RGB";
    b += "GBR";
    c += "BRG";
  }
  cin >> T;
  for (long long i = 0; i < T; i++) {
    cin >> n >> k >> s;
    ans = n;
    for (long long j = 0; j < n; j++) {
      if (s[j] != a[j])
        arr_a[j] = 1;
      else
        arr_a[j] = 0;
      if (s[j] != b[j])
        arr_b[j] = 1;
      else
        arr_b[j] = 0;
      if (s[j] != c[j])
        arr_c[j] = 1;
      else
        arr_c[j] = 0;
    }
    if (k == 1) ans = min(min(ans, arr_a[0]), min(arr_b[0], arr_c[0]));
    for (long long j = 1; j < n; j++) {
      arr_a[j] += arr_a[j - 1];
      if (j == k - 1)
        ans = min(ans, arr_a[j]);
      else if (j >= k)
        ans = min(ans, arr_a[j] - arr_a[j - k]);
      arr_b[j] += arr_b[j - 1];
      if (j == k - 1)
        ans = min(ans, arr_b[j]);
      else if (j >= k)
        ans = min(ans, arr_b[j] - arr_b[j - k]);
      arr_c[j] += arr_c[j - 1];
      if (j == k - 1)
        ans = min(ans, arr_c[j]);
      else if (j >= k)
        ans = min(ans, arr_c[j] - arr_c[j - k]);
    }
    cout << ans << endl;
  }
  return 0;
}
