#include <bits/stdc++.h>
using namespace std;
int main() {
  int q;
  cin >> q;
  for (int f = 0; f < q; f++) {
    string s[3], ss;
    int k, n;
    cin >> n >> k >> ss;
    int p[3][n];
    s[0] = ss;
    s[1] = ss;
    s[2] = ss;
    for (int i = 0; i < n; i++) {
      p[0][i] = p[1][i] = p[2][i] = 0;
    }
    if (s[0][0] != 'R') {
      p[0][0] = 1;
      s[0][0] = 'R';
    }
    if (s[1][0] != 'G') {
      p[1][0] = 1;
      s[1][0] = 'G';
    }
    if (s[2][0] != 'B') {
      p[2][0] = 1;
      s[2][0] = 'B';
    }
    for (int j = 0; j < 3; j++) {
      for (int i = 0; i < n; i++) {
        if (i > 0) {
          p[j][i] = p[j][i - 1];
          if (s[j][i - 1] == 'R' && s[j][i] != 'G') {
            p[j][i]++;
            s[j][i] = 'G';
          } else if (s[j][i - 1] == 'G' && s[j][i] != 'B') {
            p[j][i]++;
            s[j][i] = 'B';
          } else if (s[j][i - 1] == 'B' && s[j][i] != 'R') {
            p[j][i]++;
            s[j][i] = 'R';
          }
        }
      }
    }
    int ans = 10000009;
    for (int i = 0; i < n - k; i++) {
      for (int j = 0; j < 3; j++) {
        ans = min(ans, p[j][i + k] - p[j][i]);
      }
    }
    for (int j = 0; j < 3; j++) {
      ans = min(ans, p[j][k - 1]);
    }
    cout << ans << endl;
  }
  return 0;
}
