#include <bits/stdc++.h>
using namespace std;
int main() {
  int t;
  cin >> t;
  while (t--) {
    int n, k;
    cin >> n >> k;
    string s;
    cin >> s;
    string comp = "";
    for (int i = 0; i < n + 4; i++) {
      if (i % 3 == 0)
        comp += 'R';
      else if (i % 3 == 1)
        comp += 'G';
      else
        comp += 'B';
    }
    int error = INT_MAX, index = -1;
    int calc[n - k + 1][3];
    memset(calc, 0, sizeof(calc));
    for (int j = 0; j < 3; j++) {
      int temp = 0;
      for (int i = 0; i < k; i++) {
        if (s[i] != comp[i + j]) temp++;
      }
      calc[0][j] = temp;
    }
    error = min(calc[0][0], min(calc[0][1], calc[0][2]));
    for (int i = 1; i <= n - k; i++) {
      for (int j = 0; j < 3; j++) {
        calc[i][j] = calc[i - 1][j];
        if (s[i - 1] != comp[i - 1 + j]) calc[i][j]--;
        if (s[i + k - 1] != comp[i + k - 1 + j]) calc[i][j]++;
        error = min(error, calc[i][j]);
      }
    }
    cout << error << endl;
  }
  return 0;
}
