#include <bits/stdc++.h>
using namespace std;
int sum[200020], sum1[200020], sum2[200020];
int main() {
  int q;
  cin >> q;
  for (int i = 0; i < q; i++) {
    int n, k;
    string s;
    cin >> n >> k >> s;
    s = '!' + s;
    for (int j = 0; j <= n; j++) {
      sum[j] = 0;
      sum1[j] = 0;
      sum2[j] = 0;
    }
    for (int j = 1; j <= n; j++) {
      if (j % 3 == 0 && s[j] == 'R') {
        sum1[j]++;
        sum2[j]++;
      }
      if (j % 3 == 0 && s[j] == 'G') {
        sum[j]++;
        sum2[j]++;
      }
      if (j % 3 == 0 && s[j] == 'B') {
        sum[j]++;
        sum1[j]++;
      }
      if (j % 3 == 1 && s[j] == 'G') {
        sum1[j]++;
        sum2[j]++;
      }
      if (j % 3 == 1 && s[j] == 'B') {
        sum[j]++;
        sum2[j]++;
      }
      if (j % 3 == 1 && s[j] == 'R') {
        sum[j]++;
        sum1[j]++;
      }
      if (j % 3 == 2 && s[j] == 'B') {
        sum1[j]++;
        sum2[j]++;
      }
      if (j % 3 == 2 && s[j] == 'R') {
        sum[j]++;
        sum2[j]++;
      }
      if (j % 3 == 2 && s[j] == 'G') {
        sum[j]++;
        sum1[j]++;
      }
      sum[j] += sum[j - 1];
      sum1[j] += sum1[j - 1];
      sum2[j] += sum2[j - 1];
    }
    int ans = 1000000007;
    for (int j = k; j <= n; j++) {
      if (ans > sum[j] - sum[j - k]) {
        ans = sum[j] - sum[j - k];
      }
      if (ans > sum1[j] - sum1[j - k]) {
        ans = sum1[j] - sum1[j - k];
      }
      if (ans > sum2[j] - sum2[j - k]) {
        ans = sum2[j] - sum2[j - k];
      }
    }
    cout << ans << endl;
  }
}
