#include <bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f;
char dis[2000010];
int a[2000010];
int main() {
  int t;
  scanf("%d", &t);
  for (int i = 0; i < 200010; i += 3) {
    dis[i] = 'R', dis[i + 1] = 'G', dis[i + 2] = 'B';
  }
  while (t--) {
    int n, k;
    scanf("%d %d", &n, &k);
    string s;
    cin >> s;
    int minn = INF;
    int x = 0;
    while (x < 3) {
      for (int i = 0; i < n; i++) {
        if (s[i] != dis[i + x])
          a[i] = 1;
        else
          a[i] = 0;
        if (i != 0) a[i] = a[i - 1] + a[i];
      }
      x++;
      minn = min(minn, a[k - 1]);
      for (int i = k; i < n; i++) {
        minn = min(minn, a[i] - a[i - k]);
      }
    }
    printf("%d\n", minn);
  }
  return 0;
}
