#include <bits/stdc++.h>
using namespace std;
int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1};
inline int GA(long long n, vector<long long>& vec) {
  for (int i = 0; i < n; ++i) {
    long long x;
    cin >> x;
    vec.push_back(x);
  }
  return 0;
}
inline int GAEG(long long m, vector<long long> vec[]) {
  for (int i = 0; i < m; ++i) {
    long long x, y;
    cin >> x >> y;
    x--, y--;
    vec[x].push_back(y);
    vec[y].push_back(x);
  }
  return 0;
}
const long long MD = (long long)1e9 + 7;
bool isvalid(long long xx, long long yy, long long n, long long m) {
  return (xx >= 0 && xx < n && yy >= 0 && yy < m);
}
long long pw(long long x, long long y, long long dr) {
  if (y == 0) return 1;
  long long o = y / 2;
  long long f = pw(x, o, dr) % dr;
  if (y % 2) {
    return (((f * f) % dr) * x) % dr;
  } else
    return (f * f) % dr;
}
long long arr[200004];
string s = "RGB";
int main() {
  long long t;
  cin >> t;
  while (t--) {
    long long n, m;
    cin >> n >> m;
    string d;
    cin >> d;
    long long ans = 1ll << 61;
    for (int i = 0; i < 3; ++i) {
      long long qq = i;
      for (int j = 1; j < d.size() + 1; ++j) {
        arr[j] = arr[j - 1];
        if (s[qq] != d[j - 1]) arr[j]++;
        if (j >= m) ans = min(ans, arr[j] - arr[j - m]);
        qq++;
        qq %= 3;
      }
    }
    cout << ans << endl;
  }
  return 0;
}
