#include <bits/stdc++.h>
using namespace std;
int q, n, k, dif[200200];
string s, a, b, c;
int main() {
  a = b = c = "";
  for (int i = 0; i < 200200; i++) {
    if (i % 3 == 0) {
      a += 'R';
      b += 'G';
      c += 'B';
    } else if (i % 3 == 1) {
      a += 'G';
      b += 'B';
      c += 'R';
    } else {
      a += 'B';
      b += 'R';
      c += 'G';
    }
  }
  scanf("%d", &q);
  while (q--) {
    scanf("%d %d", &n, &k);
    cin >> s;
    int ans = k;
    for (int i = 0; i < n; i++) dif[i] = 0;
    for (int i = 0; i < n; i++) {
      if (s[i] != a[i]) dif[i] = 1;
      if (i) dif[i] += dif[i - 1];
    }
    for (int i = 0; i < n - k + 1; i++) {
      ans = min(ans, dif[i + k - 1] - (i == 0 ? 0 : dif[i - 1]));
    }
    for (int i = 0; i < n; i++) dif[i] = 0;
    for (int i = 0; i < n; i++) {
      if (s[i] != b[i]) dif[i] = 1;
      if (i) dif[i] += dif[i - 1];
    }
    for (int i = 0; i < n - k + 1; i++) {
      ans = min(ans, dif[i + k - 1] - (i == 0 ? 0 : dif[i - 1]));
    }
    for (int i = 0; i < n; i++) dif[i] = 0;
    for (int i = 0; i < n; i++) {
      if (s[i] != c[i]) dif[i] = 1;
      if (i) dif[i] += dif[i - 1];
    }
    for (int i = 0; i < n - k + 1; i++) {
      ans = min(ans, dif[i + k - 1] - (i == 0 ? 0 : dif[i - 1]));
    }
    printf("%d\n", ans);
  }
  return 0;
}
