#include <bits/stdc++.h>
using namespace std;
int q;
int n, k;
string s;
int a[200001], b[200001], c[200001];
int pa[200001], pb[200001], pc[200001];
int main() {
  cin.tie(0), cout.tie(0);
  ios::sync_with_stdio(false);
  cin >> q;
  while (q--) {
    cin >> n >> k;
    cin >> s;
    for (int i = 0; i < n; i++) {
      if (i % 3 == 0) {
        if (s[i] != 'R')
          a[i] = 1;
        else
          a[i] = 0;
      } else if (i % 3 == 1) {
        if (s[i] != 'G')
          a[i] = 1;
        else
          a[i] = 0;
      } else {
        if (s[i] != 'B')
          a[i] = 1;
        else
          a[i] = 0;
      }
      if (i == 0) {
        pa[i] = a[i];
      } else {
        pa[i] = pa[i - 1] + a[i];
      }
    }
    for (int i = 0; i < n; i++) {
      if (i % 3 == 0) {
        if (s[i] != 'G')
          b[i] = 1;
        else
          b[i] = 0;
      } else if (i % 3 == 1) {
        if (s[i] != 'B')
          b[i] = 1;
        else
          b[i] = 0;
      } else {
        if (s[i] != 'R')
          b[i] = 1;
        else
          b[i] = 0;
      }
      if (i == 0) {
        pb[i] = b[i];
      } else {
        pb[i] = pb[i - 1] + b[i];
      }
    }
    for (int i = 0; i < n; i++) {
      if (i % 3 == 0) {
        if (s[i] != 'B')
          c[i] = 1;
        else
          c[i] = 0;
      } else if (i % 3 == 1) {
        if (s[i] != 'R')
          c[i] = 1;
        else
          c[i] = 0;
      } else {
        if (s[i] != 'G')
          c[i] = 1;
        else
          c[i] = 0;
      }
      if (i == 0) {
        pc[i] = c[i];
      } else {
        pc[i] = pc[i - 1] + c[i];
      }
    }
    int ans = 1000001;
    for (int i = 0; i <= n - k; i++) {
      if (i == 0) {
        ans = min(ans, pa[i + k - 1]);
        ans = min(ans, pb[i + k - 1]);
        ans = min(ans, pc[i + k - 1]);
      } else {
        ans = min(ans, pa[i + k - 1] - pa[i - 1]);
        ans = min(ans, pb[i + k - 1] - pb[i - 1]);
        ans = min(ans, pc[i + k - 1] - pc[i - 1]);
      }
    }
    cout << ans << '\n';
  }
  return 0;
}
