#include <bits/stdc++.h>
const double PI = acos(-1.0);
using namespace std;
int n, k;
string str;
int amid;
string pat = "RGB";
vector<string> v;
void read_input() { cin >> n >> k >> str; }
void init() { v.clear(); }
void solve() {
  if (k == 1) {
    amid = 0;
    return;
  }
  v.push_back("RGB");
  v.push_back("GBR");
  v.push_back("BRG");
  amid = n;
  int c = 0;
  for (int l = 0; l < 3; l++) {
    string t = "";
    c = 0;
    pat = v[l];
    for (int i = 0; i < n; i++) {
      t += pat[i % 3];
    }
    for (int i = 0; i < k; i++) {
      if (str[i] != t[i]) c++;
    }
    amid = min(amid, c);
    for (int i = k; i < n; i++) {
      if (str[i - k] != t[i - k]) c--;
      if (str[i] != t[i]) c++;
      amid = min(amid, c);
    }
  }
}
void print_ans() { cout << amid << '\n'; }
int main() {
  int t = 1;
  cin >> t;
  while (t--) {
    read_input();
    solve();
    print_ans();
    if (t) init();
  }
}
