#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2")
using namespace std;
using ll = long long;
using ld = long double;
using db = double;
using str = string;
using pi = pair<ll, ll>;
using pd = pair<db, db>;
using pld = pair<ld, ld>;
using vi = vector<ll>;
using vd = vector<db>;
using vld = vector<ld>;
using vs = vector<str>;
using vb = vector<bool>;
using vc = vector<char>;
using vpi = vector<pi>;
using vpd = vector<pd>;
using vvi = vector<vi>;
using vvc = vector<vc>;
using vvs = vector<vs>;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
ll rand(ll a, ll b) { return uniform_int_distribution<ll>(a, b)(rng); }
template <class T>
bool ckmin(T& a, const T& b) {
  return a > b ? a = b, 1 : 0;
}
template <class T>
bool ckmax(T& a, const T& b) {
  return a < b ? a = b, 1 : 0;
}
template <class T>
void uni(vector<T>& v) {
  sort((v).begin(), (v).end());
  v.erase(unique((v).begin(), (v).end()), end(v));
}
constexpr long long cdiv(const ll& a, const ll& b) {
  return a / b + ((a ^ b) > 0 && a % b);
}
constexpr long long fdiv(const ll& a, const ll& b) {
  return a / b - ((a ^ b) < 0 && a % b);
}
long long power(ll a, ll b, ll c) {
  ll res = 1;
  a %= c;
  while (b > 0) {
    if (b & 1) res = res * a % c;
    a = a * a % c;
    b >>= 1;
  }
  return res;
}
long long power(ll a, ll b) {
  ll res = 1;
  while (b > 0) {
    if (b & 1) res = res * a;
    a = a * a;
    b >>= 1;
  }
  return res;
}
template <class T>
istream& operator>>(istream& is, vector<T>& hld) {
  for (long long i = 0; i < ((long long)(hld).size()); ++i) {
    cin >> hld[i];
  }
  return is;
}
template <class T>
istream& operator>>(istream& is, vector<vector<T>>& hld) {
  for (long long i = 0; i < ((long long)(hld).size()); ++i) {
    cin >> hld[i];
  }
  return is;
}
template <class T, class U>
istream& operator>>(istream& is, pair<T, U>& hld) {
  is >> hld.first >> hld.second;
  return is;
}
template <class T>
ostream& operator<<(ostream& os, vector<T>& hld) {
  for (long long i = 0; i < ((long long)(hld).size()); ++i) {
    os << hld[i];
    if (i != ((long long)(hld).size()) - 1) {
      os << ' ';
    }
  }
  return os;
}
template <class T>
ostream& operator<<(ostream& os, vector<vector<T>>& hld) {
  for (long long i = 0; i < ((long long)(hld).size()); ++i) {
    os << hld[i];
    if (i != ((long long)(hld).size()) - 1) {
      os << "\n";
    }
  }
  return os;
}
template <class T, class U>
ostream& operator<<(ostream& os, pair<T, U>& hld) {
  os << hld.first << ' ' << hld.second;
  return os;
}
void setIn(string second) { freopen(second.c_str(), "r", stdin); }
void setOut(string second) { freopen(second.c_str(), "w", stdout); }
void unsyncIO() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout << fixed << setprecision(10);
}
void setIO(string second = "") {
  unsyncIO();
  if (((long long)(second).size())) {
    setIn(second + ".in");
    setOut(second + ".out");
  }
}
const long long MX = 2e5 + 5;
const ll INF = 1e18;
const ll MOD = 1e9 + 7;
const ld PI = acos((ld)-1);
const ld EPS = 1e-8;
long long n, k, ans = INF;
string second;
vector<string> a;
vector<bitset<MX>> hld(26), hld1(26);
long long does(string t) {
  vector<long long> hld4;
  for (long long i = 0; i < ((long long)(second).size()); ++i) {
    hld[(long long)second[i] - 65][i] = 1;
  }
  for (long long i = 0; i < ((long long)(t).size()); ++i) {
    hld1[(long long)t[i] - 65][i] = 1;
  }
  for (long long i = 0;
       i <= ((long long)(second).size()) - ((long long)(t).size()); ++i) {
    long long ans1 = 0;
    for (long long j = 0; j < 26; ++j) {
      bitset<MX> hld2 = hld[j];
      hld2 &= hld1[j];
      ans1 += hld2.count();
    }
    for (long long j = 0; j < 26; ++j) {
      hld[j] >>= 1;
    }
    hld4.emplace_back(((long long)(t).size()) - ans1);
  }
  for (auto& i : hld) {
    i.reset();
  }
  for (auto& i : hld1) {
    i.reset();
  }
  return *min_element((hld4).begin(), (hld4).end());
}
void solve() {
  cin >> n >> k >> second;
  ans = INF;
  a.clear();
  a.resize(3);
  for (long long i = 0; i < k; ++i) {
    for (long long j = 0; j < 3; ++j) {
      if (i == 0) {
        if (j == 0) {
          a[j] += 'R';
        } else if (j == 1) {
          a[j] += 'G';
        } else {
          a[j] += 'B';
        }
      } else if (a[j].back() == 'R') {
        a[j] += 'G';
      } else if (a[j].back() == 'G') {
        a[j] += 'B';
      } else {
        a[j] += 'R';
      }
    }
  }
  for (long long i = 0; i < 3; ++i) {
    ckmin(ans, does(a[i]));
  }
  cout << ans << "\n";
}
int32_t main() {
  setIO();
  long long T;
  cin >> T;
  while (T--) {
    solve();
  }
}
