#include <bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f;
const int MAXN = 2e5 + 10;
char s[MAXN];
template <typename T = int>
inline T read() {
  T X = 0, w = 0;
  char ch = 0;
  while (!isdigit(ch)) {
    w |= ch == '-';
    ch = getchar();
  }
  while (isdigit(ch)) {
    X = (X << 3) + (X << 1) + (ch ^ 48);
    ch = getchar();
  }
  return w ? -X : X;
}
int dp[MAXN][3];
int main() {
  int tcnt = read();
  for (int T = 1; T <= tcnt; T++) {
    int n, k;
    cin >> n >> k;
    cin >> (s + 1);
    dp[0][0] = dp[0][1] = dp[0][2];
    char t[] = "RGB";
    for (int i = 1; i <= n; i++) {
      for (int j = 0; j < 3; j++) {
        if (s[i] == t[(i + j) % 3]) {
          dp[i][j] = dp[i - 1][j];
        } else {
          dp[i][j] = dp[i - 1][j] + 1;
        }
      }
    }
    int ans = INF;
    for (int i = 1; i + k - 1 <= n; i++) {
      for (int j = 0; j < 3; j++) {
        ans = min(ans, dp[i + k - 1][j] - dp[i - 1][j]);
      }
    }
    cout << ans << endl;
  }
}
