#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  long long q, b, c, n, k;
  cin >> q;
  while (q--) {
    cin >> n >> k;
    string s, s1 = "", s2 = "", s3 = "";
    cin >> s;
    for (int i = 0; i < k; i++) {
      if (i % 3 == 0) {
        s1 += 'R';
        s2 += 'G';
        s3 += 'B';
      } else if (i % 3 == 1) {
        s1 += 'G';
        s2 += 'B';
        s3 += 'R';
      } else {
        s1 += 'B';
        s2 += 'R';
        s3 += 'G';
      }
    }
    long long c1 = 0, c2 = 0, c3 = 0, i = 0;
    for (int j = 0; j < k; j++) {
      if (s[i + j] != s1[j]) c1++;
      if (s[i + j] != s2[j]) c2++;
      if (s[i + j] != s3[j]) c3++;
    }
    long long p1 = c1, p2 = c2, p3 = c3;
    for (int i = 1; i + k - 1 < n; i++) {
      long long y1 = p3, y2 = p1, y3 = p2;
      if (s[i - 1] != s3[0]) y1--;
      if (s[i + k - 1] != s1[k - 1]) y1++;
      if (s[i - 1] != s1[0]) y2--;
      if (s[i + k - 1] != s2[k - 1]) y2++;
      if (s[i - 1] != s2[0]) y3--;
      if (s[i + k - 1] != s3[k - 1]) y3++;
      p1 = y1;
      p2 = y2;
      p3 = y3;
      c1 = min(c1, y1);
      c2 = min(c2, y2);
      c3 = min(c3, y3);
    }
    cout << min(c1, min(c2, c3)) << "\n";
  }
}
