#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 20;
int q, n, k, dp[N], ans;
char a[N], s[] = "RGB";
void f(int j) {
  for (int i = 1; i <= n; i++) {
    dp[i] = dp[i - 1] + (a[i] != s[j]);
    if (i >= k) ans = min(ans, dp[i] - dp[i - k]);
    j = (j + 1) % 3;
  }
}
int main() {
  scanf("%d", &q);
  while (q--) {
    scanf("%d%d %s", &n, &k, a + 1);
    ans = n;
    for (int j = 0; j < 3; j++) f(j);
    printf("%d\n", ans);
  }
}
