#include <bits/stdc++.h>
using namespace std;
string a = "RGBRGBRGBRGBRGBRGBRGBRGBRGBRGB",
       b = "GBRGBRGBRGBRGBRGBRGBRGBRGBRGBR";
string c = "BRGBRGBRGBRGBRGBRGBRGBRGBRGBRG";
string s;
string a1, a2, a3;
int main() {
  for (int i = 1; i <= 6667; i++) {
    a1 += a, a2 += b, a3 += c;
  }
  int t;
  scanf("%d", &t);
  while (t--) {
    int n, k;
    scanf("%d %d", &n, &k);
    cin >> s;
    int ans = 999999999;
    vector<vector<int>> dp(3, vector<int>(n, 0));
    for (int i = 0; i < n; i++) {
      if (s[i] != a1[i]) dp[0][i]++;
      if (s[i] != a2[i]) dp[1][i]++;
      if (s[i] != a3[i]) dp[2][i]++;
    }
    for (int i = 0; i < 3; i++) {
      int count = 0;
      for (int j = 0; j < k; j++) count += dp[i][j];
      ans = min(ans, count);
      for (int j = k; j < n; j++) {
        count -= dp[i][j - k];
        count += dp[i][j];
        ans = min(ans, count);
      }
    }
    printf("%d\n", ans);
  }
}
