#include <bits/stdc++.h>
using namespace std;
int dx[8] = {-1, -1, -1, 0, 1, 0, 1, 1};
int dy[8] = {-1, 0, 1, -1, -1, 1, 1, 0};
long long int visited[200000];
vector<long long int> adj[200000];
long long int n, m;
long long int level[200000];
long long int ch = false;
vector<long long int> final_ans;
vector<long long int> dfs(int i, vector<long long int> ans, int countagain,
                          int findvalue) {
  if (i == findvalue && countagain == 0) {
    countagain = 1;
    ans.push_back(i);
  } else {
    visited[i] = 1;
  }
  for (auto s : adj[i]) {
    if (!visited[s]) {
      ans.push_back(s);
      dfs(s, ans, countagain, findvalue);
      if (ans.size() >= 4 && ans[ans.size() - 1] == findvalue) {
        return ans;
      } else {
        ans.pop_back();
      }
    }
  }
  return ans;
}
void solve() {
  long long int n;
  cin >> n;
  long long int k;
  cin >> k;
  string s1 = "", s2 = "", s3 = "";
  for (int i = 0; i < n; i++) {
    if (i % 3 == 0)
      s1 += 'R';
    else if (i % 3 == 1)
      s1 += 'G';
    else if (i % 3 == 2)
      s1 += 'B';
  }
  for (int i = 0; i < n; i++) {
    if (i % 3 == 0)
      s2 += 'G';
    else if (i % 3 == 1)
      s2 += 'B';
    else if (i % 3 == 2)
      s2 += 'R';
  }
  for (int i = 0; i < n; i++) {
    if (i % 3 == 0)
      s3 += 'B';
    else if (i % 3 == 1)
      s3 += 'R';
    else if (i % 3 == 2)
      s3 += 'G';
  }
  string s;
  cin >> s;
  long long int ans = 1e9;
  string maincount1 = "", maincount2 = "", maincount3 = "";
  int id = 0;
  for (int i = 0; i < n; i++) {
    if (s[i] != s1[i]) {
      maincount1 += '0';
    } else {
      maincount1 += '1';
    }
    if (s[i] != s2[i]) {
      maincount2 += '0';
    } else {
      maincount2 += '1';
    }
    if (s[i] != s3[i]) {
      maincount3 += '0';
    } else {
      maincount3 += '1';
    }
    id++;
  }
  long long int mx1 = 0, mx2 = 0, mx3 = 0;
  long long int count1 = 0, count2 = 0, count3 = 0;
  for (int i = 0; i < k; i++) {
    if (maincount1[i] == '1') {
      count1++;
    }
    if (maincount2[i] == '1') {
      count2++;
    }
    if (maincount3[i] == '1') {
      count3++;
    }
  }
  mx1 = max(mx1, count1);
  mx2 = max(mx2, count2);
  mx3 = max(mx3, count3);
  for (int i = k; i < n; i++) {
    count1 -= maincount1[i - k] - '0';
    count1 += maincount1[i] - '0';
    mx1 = max(mx1, count1);
    count2 -= maincount2[i - k] - '0';
    count2 += maincount2[i] - '0';
    mx2 = max(mx2, count2);
    count3 -= maincount3[i - k] - '0';
    count3 += maincount3[i] - '0';
    mx3 = max(mx3, count3);
  }
  long long int final_mx = max({mx1, mx2, mx3});
  cout << ((final_mx >= k) ? 0 : abs(final_mx - k)) << endl;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(0);
  int t;
  cin >> t;
  while (t--) {
    solve();
  }
}
