#include <bits/stdc++.h>
using namespace std;
bool sortbydesc(long long int a, long long int b) { return a > b; }
class Graph {
  int V;
  list<int> *adj;

 public:
  Graph(int V);
  void addEdge(int v, int w);
  void DFSUtil(int v, bool *visited, std::vector<int> &v1);
};
Graph::Graph(int V) {
  this->V = V;
  adj = new list<int>[V];
}
void Graph::addEdge(int v, int w) { adj[v].push_back(w); }
void Graph::DFSUtil(int v, bool *visited, vector<int> &v1) {
  visited[v] = true;
  v1.push_back(v);
  list<int>::iterator i;
  for (i = adj[v].begin(); i != adj[v].end(); ++i)
    if (!visited[*i]) DFSUtil(*i, visited, v1);
}
int maxSum(int *arr, int n, int k) {
  if (n < k) {
    cout << "Invalid";
    return -1;
  }
  int res = 0;
  for (int i = 0; i < k; i++) res += arr[i];
  int curr_sum = res;
  for (int i = k; i < n; i++) {
    curr_sum += arr[i] - arr[i - k];
    res = min(res, curr_sum);
  }
  return res;
}
int main() {
  int t;
  cin >> t;
  for (int p = 0; p < t; p++) {
    int n, k;
    cin >> n >> k;
    string s;
    cin >> s;
    int min1 = 2001;
    int min2;
    int min3;
    int min4;
    vector<char> v1, v2, v3;
    for (int i = 0; i < n; i++) {
      if (i % 3 == 0) {
        v1.push_back('R');
      } else if (i % 3 == 1) {
        v1.push_back('G');
      } else {
        v1.push_back('B');
      }
    }
    for (int i = 0; i < n; i++) {
      if (i % 3 == 0) {
        v2.push_back('G');
      } else if (i % 3 == 1) {
        v2.push_back('B');
      } else {
        v2.push_back('R');
      }
    }
    for (int i = 0; i < n; i++) {
      if (i % 3 == 0) {
        v3.push_back('B');
      } else if (i % 3 == 1) {
        v3.push_back('R');
      } else {
        v3.push_back('G');
      }
    }
    int *a1 = new int[n];
    int *a2 = new int[n];
    int *a3 = new int[n];
    for (int i = 0; i < n; i++) {
      a1[i] = 0;
      a2[i] = 0;
      a3[i] = 0;
    }
    for (int i = 0; i < n; i++) {
      if (s[i] != v1[i]) {
        a1[i]++;
      }
      if (s[i] != v2[i]) {
        a2[i]++;
      }
      if (s[i] != v3[i]) {
        a3[i]++;
      }
    }
    min2 = maxSum(a1, n, k);
    min3 = maxSum(a2, n, k);
    min4 = maxSum(a3, n, k);
    min1 = min(min2, min(min3, min4));
    cout << min1 << endl;
  }
  return 0;
}
