#include <bits/stdc++.h>
using namespace std;
const int M = 2e5 + 10;
char s[M];
int a[3][M];
int T, n, k, ans;
int main() {
  cin >> T;
  while (T--) {
    a[0][0] = a[1][0] = a[2][0] = 0;
    cin >> n >> k;
    cin >> s;
    ans = M;
    string t = "RGB";
    for (int i = 0; i < n; i++) {
      for (int j = 0; j <= 2; j++) {
        if (s[i] != t[(i + j) % 3])
          a[j][i + 1] = a[j][i] + 1;
        else
          a[j][i + 1] = a[j][i];
      }
    }
    for (int i = 0; i + k <= n; i++) {
      for (int j = 0; j <= 2; j++) {
        ans = min(ans, a[j][i + k] - a[j][i]);
      }
    }
    cout << ans << endl;
  }
  return 0;
}
