#include <bits/stdc++.h>
using namespace std;
const double eps = 1e-8;
const double pi = acos(-1);
const long long inf = 0x3f3f3f3f3f3f3f3f;
const int INF = 0x3f3f3f3f;
const int MAX = 2e5 + 10;
const long long mod = 1e9 + 7;
long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; }
char c[3] = {'R', 'G', 'B'};
int main() {
  int t;
  cin >> t;
  while (t--) {
    int dif[3] = {0};
    int n, k;
    cin >> n >> k;
    string s;
    cin >> s;
    for (int i = (0); i < (3); ++i) {
      for (int j = (0); j < (k); ++j) {
        if (s[j] != c[(j + i) % 3]) dif[i]++;
      }
    }
    int ans = 0;
    ans = min(dif[0], min(dif[1], dif[2]));
    for (int i = 1; i + k - 1 < n; i++) {
      int a0 = dif[0], a1 = dif[1], a2 = dif[2];
      dif[0] = a2 - (s[i - 1] == 'B' ? 0 : 1) +
               (s[i + k - 1] == c[(k - 1) % 3] ? 0 : 1);
      dif[1] =
          a0 - (s[i - 1] == 'R' ? 0 : 1) + ((s[i + k - 1] == c[k % 3]) ? 0 : 1);
      dif[2] = a1 - (s[i - 1] == 'G' ? 0 : 1) +
               ((s[i + k - 1] == c[(k + 1) % 3]) ? 0 : 1);
      ans = min(ans, min(dif[0], min(dif[1], dif[2])));
    }
    cout << ans << '\n';
  }
}
