#include <bits/stdc++.h>
using namespace std;
char d1[200005];
char d2[200005];
char d3[200005];
long long arr[200005];
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  ;
  for (long long i = 0; i < 200005; i++) {
    if (i % 3 == 0) d1[i] = 'R';
    if (i % 3 == 1) d1[i] = 'G';
    if (i % 3 == 2) d1[i] = 'B';
  }
  for (long long i = 0; i < 200005; i++) {
    if (i % 3 == 0) d2[i] = 'G';
    if (i % 3 == 1) d2[i] = 'B';
    if (i % 3 == 2) d2[i] = 'R';
  }
  for (long long i = 0; i < 200005; i++) {
    if (i % 3 == 0) d3[i] = 'B';
    if (i % 3 == 1) d3[i] = 'R';
    if (i % 3 == 2) d3[i] = 'G';
  }
  string s;
  long long q, n, k, mi, ans;
  cin >> q;
  for (long long i = 0; i < q; i++) {
    cin >> n >> k;
    getline(cin, s);
    getline(cin, s);
    for (long long j = 0; j < n; j++) {
      if (d1[j] == s[j])
        arr[j] = 0;
      else
        arr[j] = 1;
    }
    for (long long j = 1; j < n; j++) arr[j] = arr[j - 1] + arr[j];
    mi = arr[k - 1];
    for (long long j = k; j < n; j++) {
      if (arr[j] - arr[j - k] < mi) mi = arr[j] - arr[j - k];
    }
    ans = mi;
    for (long long j = 0; j < n; j++) {
      if (d2[j] == s[j])
        arr[j] = 0;
      else
        arr[j] = 1;
    }
    for (long long j = 1; j < n; j++) arr[j] = arr[j - 1] + arr[j];
    mi = arr[k - 1];
    for (long long j = k; j < n; j++) {
      if (arr[j] - arr[j - k] < mi) mi = arr[j] - arr[j - k];
    }
    ans = min(ans, mi);
    for (long long j = 0; j < n; j++) {
      if (d3[j] == s[j])
        arr[j] = 0;
      else
        arr[j] = 1;
    }
    for (long long j = 1; j < n; j++) arr[j] = arr[j - 1] + arr[j];
    mi = arr[k - 1];
    for (long long j = k; j < n; j++) {
      if (arr[j] - arr[j - k] < mi) mi = arr[j] - arr[j - k];
    }
    ans = min(ans, mi);
    cout << ans << '\n';
  }
  return 0;
}
