#include <bits/stdc++.h>
int T, n, k, a[200005], s[200005][3], ans;
char tmp[200005];
int main() {
  scanf("%d", &T);
  while (T--) {
    ans = 0x3f3f3f3f;
    scanf("%d%d%s", &n, &k, tmp);
    for (int i = 0; i < n; ++i)
      a[i + 1] = tmp[i] == 'R' ? 0 : (tmp[i] == 'G' ? 1 : 2);
    for (int i = 1; i <= n; ++i)
      s[i][0] = (a[i] == (i + 2) % 3 ? 0 : 1) + s[i - 1][0],
      s[i][1] = (a[i] == (i + 1) % 3 ? 0 : 1) + s[i - 1][1],
      s[i][2] = (a[i] == i % 3 ? 0 : 1) + s[i - 1][2];
    for (int i = 1; i <= n - k + 1; ++i)
      ans = std::min(ans, s[i + k - 1][0] - s[i - 1][0]);
    for (int i = 1; i <= n - k + 1; ++i)
      ans = std::min(ans, s[i + k - 1][1] - s[i - 1][1]);
    for (int i = 1; i <= n - k + 1; ++i)
      ans = std::min(ans, s[i + k - 1][2] - s[i - 1][2]);
    printf("%d\n", ans);
  }
  return 0;
}
