#include <bits/stdc++.h>
using namespace std;
const long long int N = 500005;
const long long int p = 31;
long long int n, c;
signed main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  string mt = "RGB";
  long long int q;
  cin >> q;
  while (q--) {
    long long int n, k;
    cin >> n >> k;
    long long int dp[n + 10][3];
    for (long long int i = 0; i < n + 5; i++) {
      for (long long int j = 0; j < 3; j++) {
        dp[i][j] = 0;
      }
    }
    string s;
    cin >> s;
    if (s[0] != mt[0]) {
      dp[0][0] = 1;
    }
    if (s[0] != mt[1]) {
      dp[0][1] = 1;
    }
    if (s[0] != mt[2]) {
      dp[0][2] = 1;
    }
    long long int off;
    off = 1;
    for (long long int i = 1; i < n; i++, off++) {
      if (s[i] != mt[off % 3]) {
        dp[i][0]++;
      }
      if (i != 0) {
        dp[i][0] += dp[i - 1][0];
      }
    }
    off = 2;
    for (long long int i = 1; i < n; i++, off++) {
      if (s[i] != mt[off % 3]) {
        dp[i][1]++;
      }
      if (i != 0) {
        dp[i][1] += dp[i - 1][1];
      }
    }
    off = 3;
    for (long long int i = 1; i < n; i++, off++) {
      if (s[i] != mt[off % 3]) {
        dp[i][2]++;
      }
      if (i != 0) {
        dp[i][2] += dp[i - 1][2];
      }
    }
    long long int mn = 100000000000000;
    mn = min(mn, dp[k - 1][0]);
    mn = min(mn, dp[k - 1][1]);
    mn = min(mn, dp[k - 1][2]);
    for (long long int i = k; i < n; i++) {
      mn = min(mn, dp[i][0] - dp[i - k][0]);
      mn = min(mn, dp[i][1] - dp[i - k][1]);
      mn = min(mn, dp[i][2] - dp[i - k][2]);
    }
    cout << mn << endl;
  }
  return 0;
}
