#include <bits/stdc++.h>
using namespace std;
int n, k, mem[200005], s[200005], m[200005], sum[200005], t, res1, res2, res3;
char rr;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> t;
  while (t--) {
    cin >> n >> k;
    k;
    for (int i = 1; i <= n; i++) {
      cin >> rr;
      if (rr == 'R') s[i] = 0;
      if (rr == 'G') s[i] = 1;
      if (rr == 'B') s[i] = 2;
    }
    res1 = 1000000000;
    for (int i = 1; i <= n; i++) m[i] = i % 3;
    for (int i = 1; i <= n; i++) {
      if (m[i] != s[i])
        sum[i] = 1;
      else
        sum[i] = 0;
      sum[i] += sum[i - 1];
    }
    for (int i = k; i <= n; i++) res1 = min(res1, sum[i] - sum[i - k]);
    res2 = 1000000000;
    for (int i = 1; i <= n; i++) m[i] = (i + 1) % 3;
    for (int i = 1; i <= n; i++) {
      if (m[i] != s[i])
        sum[i] = 1;
      else
        sum[i] = 0;
      sum[i] += sum[i - 1];
    }
    for (int i = k; i <= n; i++) res2 = min(res2, sum[i] - sum[i - k]);
    res3 = 1000000000;
    for (int i = 1; i <= n; i++) m[i] = (i + 2) % 3;
    for (int i = 1; i <= n; i++) {
      if (m[i] != s[i])
        sum[i] = 1;
      else
        sum[i] = 0;
      sum[i] += sum[i - 1];
    }
    for (int i = k; i <= n; i++) res3 = min(res3, sum[i] - sum[i - k]);
    cout << min(min(res1, res2), res3) << '\n';
  }
}
