#include <bits/stdc++.h>
using namespace std;
const long long int MOD = 1e9 + 7;
long long int dp[(long long int)2e5 + 4][3];
long long int get(long long int j) {
  if (j < 0) return (j + 3);
  return j;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int t = 1;
  cin >> t;
  while (t--) {
    long long int n, k, i;
    cin >> n >> k;
    string s;
    cin >> s;
    dp[0][0] = dp[0][1] = dp[0][2] = 0;
    if (s[0] == 'R')
      dp[0][1] = dp[0][2] = 1;
    else if (s[0] == 'G')
      dp[0][0] = dp[0][2] = 1;
    else
      dp[0][0] = dp[0][1] = 1;
    for (i = 1; i < n; i++) {
      if (s[i] == 'R') {
        dp[i][0] = dp[i - 1][2];
        dp[i][1] = 1 + dp[i - 1][0];
        dp[i][2] = 1 + dp[i - 1][1];
      } else if (s[i] == 'G') {
        dp[i][0] = 1 + dp[i - 1][2];
        dp[i][1] = dp[i - 1][0];
        dp[i][2] = 1 + dp[i - 1][1];
      } else {
        dp[i][0] = 1 + dp[i - 1][2];
        dp[i][1] = 1 + dp[i - 1][0];
        dp[i][2] = dp[i - 1][1];
      }
    }
    long long int ans = 1e18, j, k1 = k;
    k %= 3;
    for (i = k1 - 1; i < n; i++) {
      if (i == k1 - 1) {
        ans = min(ans, min(dp[i][0], min(dp[i][1], dp[i][2])));
        continue;
      }
      j = 0;
      j -= k;
      j = get(j);
      ans = min(ans, dp[i][0] - dp[i - k1][j]);
      j = 1;
      j -= k;
      j = get(j);
      ans = min(ans, dp[i][1] - dp[i - k1][j]);
      j = 2;
      j -= k;
      j = get(j);
      ans = min(ans, dp[i][2] - dp[i - k1][j]);
    }
    cout << ans << "\n";
  }
  return 0;
}
