#include <bits/stdc++.h>
using namespace std;
int main() {
  int T;
  cin >> T;
  for (int h = 0; h < T; h++) {
    int n, k;
    cin >> n >> k;
    string s;
    cin >> s;
    int a[3][n];
    for (int i = 0; i < n; i++) {
      (s[i] == 'R') ? a[0][i] = 0 : a[0][i] = 1;
      (s[i] == 'G') ? a[1][i] = 0 : a[1][i] = 1;
      (s[i] == 'B') ? a[2][i] = 0 : a[2][i] = 1;
    }
    int check[3][n - k + 1], min = 100000;
    for (int t = 0; t < 3; t++) {
      check[t][0] = 0;
      int m = t;
      for (int i = 0; i < k; i++) {
        check[t][0] += a[m][i];
        m++;
        if (m >= 3) m = 0;
      }
      if (min > check[t][0]) min = check[t][0];
    }
    for (int i = 1; i <= n - k; i++) {
      for (int t = 0; t < 3; t++) {
        (t == 0) ? check[t][i] =
                       check[2][i - 1] - a[2][i - 1] + a[(2 + k) % 3][k + i - 1]
                 : check[t][i] = check[t - 1][i - 1] - a[t - 1][i - 1] +
                                 a[(k - 1 + t) % 3][k + i - 1];
        if (min > check[t][i]) min = check[t][i];
      }
    }
    cout << min << endl;
  }
}
