#include <bits/stdc++.h>
using namespace std;
int main() {
  long long q;
  cin >> q;
  while (q--) {
    long long n, k;
    cin >> n >> k;
    string s;
    cin >> s;
    vector<int> p1(n + 4), p2(n + 4), p3(n + 3), a(n + 2);
    for (int i = 1; i <= n; i++)
      a[i] = (s[i - 1] == 'R' ? 1 : (s[i - 1] == 'G' ? 2 : 3));
    int c1 = 1, c2 = 2, c3 = 3;
    for (int i = 1; i <= n; i++) {
      p1[i] = p1[i - 1];
      p2[i] = p2[i - 1];
      p3[i] = p3[i - 1];
      if (a[i] != c1) p1[i]++;
      if (a[i] != c2) p2[i] += 1;
      if (a[i] != c3) p3[i] += 1;
      c1 = c1 % 3 + 1;
      c2 = c2 % 3 + 1;
      c3 = c3 % 3 + 1;
    }
    int mn = INT_MAX;
    for (int i = 1; i + k - 1 <= n; i++) {
      int v1 = p1[i + k - 1] - p1[i - 1], v2 = p2[i + k - 1] - p2[i - 1],
          v3 = p3[i + k - 1] - p3[i - 1];
      mn = min(mn, min(v1, min(v2, v3)));
    }
    cout << mn << "\n";
  }
}
