#include <bits/stdc++.h>
using namespace std;
int main() {
  int q;
  cin >> q;
  while (q--) {
    int n, m;
    cin >> n >> m;
    string second;
    cin >> second;
    vector<int> v(n);
    for (int i = 0; i < n; i++) {
      if (second[i] == 'R') v[i] = 0;
      if (second[i] == 'G') v[i] = 1;
      if (second[i] == 'B') v[i] = 2;
    }
    vector<int> dp[3];
    dp[0].resize(n, 0);
    dp[1].resize(n, 0);
    dp[2].resize(n, 0);
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < 3; j++) {
        dp[j][i] = (i + j) % 3;
      }
    }
    cout << endl;
    int ans = 0x7fffffff;
    int cnt[3] = {0};
    for (int i = 0; i < m; i++) {
      int num = v[i];
      for (int j = 0; j < 3; j++) {
        if (num != dp[j][i]) {
          cnt[j]++;
        }
      }
    }
    if (min(cnt[0], min(cnt[1], cnt[2])) < ans) {
      ans = min(cnt[0], min(cnt[1], cnt[2]));
    }
    for (int i = 1; i < n - m + 1; i++) {
      int lastVal = v[i - 1];
      int next = v[i + m - 1];
      for (int j = 0; j < 3; j++) {
        if (lastVal != dp[j][i - 1]) {
          cnt[j]--;
        }
      }
      for (int j = 0; j < 3; j++) {
        if (next != dp[j][i + m - 1]) {
          cnt[j]++;
        }
      }
      if (min(cnt[0], min(cnt[1], cnt[2])) < ans) {
        ans = min(cnt[0], min(cnt[1], cnt[2]));
      }
    }
    cout << ans << endl;
  }
}
