#include <bits/stdc++.h>
using namespace std;
void ECHO(string _s) {
  cout << endl;
  (void)_s;
}
template <typename T, typename... Args>
void ECHO(string _s, T x, Args... args) {
  int _i;
  string _s2 = "";
  for (_i = 0; _i < (int)(_s).size(); ++_i) {
    if (_s[_i] == ',') break;
    if (_s[_i] != ' ') _s2 += _s[_i];
  }
  if (_i == (int)(_s).size()) {
    --_i;
  }
  cout << "  (" << _s2 << "):" << x;
  ECHO(_s.substr(_i + 1, (int)(_s).size() - _i - 1), args...);
}
template <typename T0, typename T1>
inline ostream& operator<<(ostream& os, pair<T0, T1>& p) {
  return os << "(" << p.first << ", " << p.second << ")";
}
template <typename T>
inline ostream& operator<<(ostream& os, vector<T>& v) {
  for (int i = 0; i < (int)(v).size(); ++i) cout << v[i] << "_";
  return os;
}
template <typename T1, typename T2>
inline void ckmin(T1& x1, T2 x2) {
  if (x2 < x1) x1 = x2;
}
template <typename T1, typename T2>
inline void ckmax(T1& x1, T2 x2) {
  if (x2 > x1) x1 = x2;
}
inline long long _gcd(long long a, long long b) {
  while (b) b %= a ^= b ^= a ^= b;
  return a;
}
mt19937 ran(time(0));
const long double PI = acos(-1);
const long double eps = 1e-9;
const long long R = 0;
const long long G = 1;
const long long B = 2;
int main() {
  ios::sync_with_stdio(false);
  long long i, n, k, m, t;
  string s;
  cin >> t;
  while (t--) {
    cin >> n >> k;
    cin >> s;
    if (k == 1) {
      cout << "0\n";
      continue;
    }
    m = k;
    vector<vector<long long> > a(3, vector<long long>(3, 0));
    for (i = 0; i < k; ++i) {
      if (s[i] == 'R') ++a[R][i % 3];
      if (s[i] == 'G') ++a[G][i % 3];
      if (s[i] == 'B') ++a[B][i % 3];
    }
    m = min(m, k - (a[R][0] + a[G][1] + a[B][2]));
    m = min(m, k - (a[R][1] + a[G][2] + a[B][0]));
    m = min(m, k - (a[R][2] + a[G][0] + a[B][1]));
    for (; i < n; ++i) {
      if (s[i - k] == 'R') --a[R][0];
      if (s[i - k] == 'G') --a[G][0];
      if (s[i - k] == 'B') --a[B][0];
      if (k == 2) {
        for (int z = 0; z < 3; ++z) {
          swap(a[z][0], a[z][1]);
        }
      } else {
        for (int z = 0; z < 3; ++z) {
          swap(a[z][0], a[z][1]);
          swap(a[z][1], a[z][2]);
        }
      }
      if (s[i] == 'R') ++a[R][((k % 3) - 1 + 3) % 3];
      if (s[i] == 'G') ++a[G][((k % 3) - 1 + 3) % 3];
      if (s[i] == 'B') ++a[B][((k % 3) - 1 + 3) % 3];
      m = min(m, k - (a[R][0] + a[G][1] + a[B][2]));
      m = min(m, k - (a[R][1] + a[G][2] + a[B][0]));
      m = min(m, k - (a[R][2] + a[G][0] + a[B][1]));
    }
    cout << m << "\n";
  }
  return 0;
}
