#include <bits/stdc++.h>
using namespace std;
int r[200010];
int g[200010];
int b[200010];
int sr[200010];
int sg[200010];
int sb[200010];
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int test;
  cin >> test;
  for (int x = 1; x <= test; x++) {
    int n, k;
    cin >> n >> k;
    for (int i = 1; i <= n; i++) {
      r[i] = 0;
      g[i] = 0;
      b[i] = 0;
      sr[i] = 0;
      sg[i] = 0;
      sb[i] = 0;
    }
    string s;
    cin >> s;
    for (int i = 1; i <= n; i++) {
      if (i % 3 == 1) {
        if (s[i - 1] == 'R')
          r[i] = 1;
        else if (s[i - 1] == 'G')
          g[i] = 1;
        else if (s[i - 1] == 'B')
          b[i] = 1;
      } else if (i % 3 == 2) {
        if (s[i - 1] == 'G')
          r[i] = 1;
        else if (s[i - 1] == 'B')
          g[i] = 1;
        else if (s[i - 1] == 'R')
          b[i] = 1;
      } else if (i % 3 == 0) {
        if (s[i - 1] == 'B')
          r[i] = 1;
        else if (s[i - 1] == 'R')
          g[i] = 1;
        else if (s[i - 1] == 'G')
          b[i] = 1;
      }
    }
    for (int i = 1; i <= n; i++) {
      sr[i] = sr[i - 1] + r[i];
      sg[i] = sg[i - 1] + g[i];
      sb[i] = sb[i - 1] + b[i];
    }
    int ans = 1e9 + 7;
    for (int i = k; i <= n; i++) {
      ans = min(ans, k - (sr[i] - sr[i - k]));
      ans = min(ans, k - (sg[i] - sg[i - k]));
      ans = min(ans, k - (sb[i] - sb[i - k]));
    }
    cout << ans << '\n';
  }
}
