#include <bits/stdc++.h>
using namespace std;
template <typename Q>
inline char inin(Q &x) {
  x = 0;
  int f = 0;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-') f = 1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') x = x * 10 + ch - '0', ch = getchar();
  x = f ? -x : x;
  return ch;
}
int t, n, k;
int f[2222][2222][4];
char tt[5] = {'0', 'B', 'G', 'R'};
int main() {
  inin(t);
  while (t--) {
    inin(n), inin(k);
    char s[2222];
    scanf("%s", (s + 1));
    for (int i = (1); i <= (3); i++)
      if (tt[i] == s[1])
        f[1][1][i] = 0;
      else
        f[1][1][i] = 1;
    for (int i = (2); i <= (n); i++) {
      for (int j = (1); j <= (min(i, k)); j++) {
        for (int k = (1); k <= (3); k++)
          if (s[i] == tt[k])
            f[i][j][k] = f[i - 1][j - 1][k % 3 + 1];
          else
            f[i][j][k] = f[i - 1][j - 1][k % 3 + 1] + 1;
      }
    }
    int ans = 2147483647;
    for (int i = (k); i <= (n); i++)
      for (int j = (1); j <= (3); j++) ans = min(ans, f[i][k][j]);
    cout << ans << "\n";
  }
  return 0;
}
