#include <bits/stdc++.h>
using namespace std;
const int inf = 1e9 + 7;
long long int arr[200005];
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int t;
  cin >> t;
  while (t--) {
    int n, k;
    cin >> n >> k;
    string st;
    cin >> st;
    int pre[3][200005];
    memset(pre, 0, sizeof(pre));
    for (int i = 0; i < n; i++) {
      if (i % 3 == 0 and st[i] != 'R') pre[0][i + 1] = 1;
      if (i % 3 == 1 and st[i] != 'G') pre[0][i + 1] = 1;
      if (i % 3 == 2 and st[i] != 'B') pre[0][i + 1] = 1;
    }
    for (int i = 0; i < n; i++) {
      if (i % 3 == 0 and st[i] != 'G') pre[1][i + 1] = 1;
      if (i % 3 == 1 and st[i] != 'B') pre[1][i + 1] = 1;
      if (i % 3 == 2 and st[i] != 'R') pre[1][i + 1] = 1;
    }
    for (int i = 0; i < n; i++) {
      if (i % 3 == 0 and st[i] != 'B') pre[2][i + 1] = 1;
      if (i % 3 == 1 and st[i] != 'R') pre[2][i + 1] = 1;
      if (i % 3 == 2 and st[i] != 'G') pre[2][i + 1] = 1;
    }
    for (int j = 0; j < 3; j++)
      for (int i = 1; i <= n; i++) pre[j][i] += pre[j][i - 1];
    int cnt = inf;
    for (int i = 0; i <= n - k; i++) {
      cnt = min(cnt, pre[0][i + k] - pre[0][i]);
      cnt = min(cnt, pre[1][i + k] - pre[1][i]);
      cnt = min(cnt, pre[2][i + k] - pre[2][i]);
    }
    cout << cnt << endl;
  }
}
