#include <bits/stdc++.h>
using namespace std;
namespace io {
inline void readl(long long &x) {
  x = 0;
  long long f = 1;
  char c = getchar();
  while (c < '0' || c > '9') {
    if (c == '-') f = -f;
    c = getchar();
  }
  while (c >= '0' && c <= '9') {
    x = x * 10 + c - '0';
    c = getchar();
  }
  x *= f;
}
inline void read(int &x) {
  x = 0;
  int f = 1;
  char c = getchar();
  while (c < '0' || c > '9') {
    if (c == '-') f = -f;
    c = getchar();
  }
  while (c >= '0' && c <= '9') {
    x = x * 10 + c - '0';
    c = getchar();
  }
  x *= f;
}
}  // namespace io
using namespace io;
int q;
int n, k;
char str[3] = {'R', 'G', 'B'};
char s[200005];
int sum[200005];
int solve() {
  int res = k;
  for (register int i = 1; i <= n; i++) {
    sum[i] += sum[i - 1];
    if (i >= k) res = min(res, sum[i] - sum[i - k]);
  }
  return res;
}
int main() {
  read(q);
  while (q--) {
    read(n), read(k);
    scanf("%s", s + 1);
    int cur = 0, ans = k;
    for (register int i = 1; i <= n; i++, cur = (cur + 1) % 3) {
      if (s[i] != str[cur])
        sum[i] = 1;
      else
        sum[i] = 0;
    }
    ans = min(ans, solve());
    cur = 1;
    for (register int i = 1; i <= n; i++, cur = (cur + 1) % 3) {
      if (s[i] != str[cur])
        sum[i] = 1;
      else
        sum[i] = 0;
    }
    ans = min(ans, solve());
    cur = 2;
    for (register int i = 1; i <= n; i++, cur = (cur + 1) % 3) {
      if (s[i] != str[cur])
        sum[i] = 1;
      else
        sum[i] = 0;
    }
    ans = min(ans, solve());
    printf("%d\n", ans);
  }
  return 0;
}
