#include <bits/stdc++.h>
using namespace std;
int sum[200006][3], q, n, k, dp[200006][3], a[200006];
char s[200006];
inline int f(int a) {
  if (a == 'R') return 0;
  if (a == 'G') return 1;
  if (a == 'B') return 2;
}
int main(void) {
  scanf("%d", &q);
  while (q--) {
    scanf("%d%d", &n, &k);
    scanf("%s", s + 1);
    for (int i = 1; i <= n; i++) {
      a[i] = (3 + f(s[i]) - i % 3) % 3;
      for (int j = 0; j < 3; j++)
        sum[i][j] = (j == a[i] ? sum[i - 1][j] + 1 : sum[i - 1][j]);
      dp[i][0] = dp[i][1] = dp[i][2] = 0;
    }
    for (int i = 1; i < k; i++) dp[i][0] = dp[i][1] = dp[i][2] = 200006;
    for (int i = k; i <= n; i++) {
      for (int j = 0; j < 3; j++) {
        dp[i][j] = min(dp[i - 1][j], k - (sum[i][j] - sum[i - k][j]));
      }
    }
    int ans = 200006;
    for (int i = 0; i < 3; i++) ans = min(ans, dp[n][i]);
    cout << ans << endl;
  }
}
