#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  char C[128] = {};
  C['R'] = 0, C['G'] = 1, C['B'] = 2;
  int T;
  cin >> T;
  while (T--) {
    int N, M, m = 1e9;
    string s;
    cin >> N >> M >> s;
    vector<vector<int>> A(3, vector<int>(N + 1));
    for (int j = 0; j < 3; j++)
      for (int i = 0; i < N; i++) {
        char c = C[s[i]];
        A[j][i + 1] = c != (i + j) % 3;
      }
    for (int j = 0; j < 3; j++)
      for (int i = 1; i < N + 1; i++) A[j][i] += A[j][i - 1];
    for (int j = 0; j < 3; j++)
      for (int i = M; i < N + 1; i++) m = min(m, A[j][i] - A[j][i - M]);
    cout << m << '\n';
  }
}
