#include <iostream>
#include <iomanip>
#include <sstream>
#include <cstdio>
#include <string>
#include <vector>
#include <algorithm>
#include <complex>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <cassert>
#include <climits>
#include <queue>
#include <set>
#include <map>
#include <valarray>
#include <bitset>
#include <stack>
using namespace std;

#define REP(i,n) for(int i=0;i<(int)n;++i)
#define FOR(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i)
#define ALL(c) (c).begin(), (c).end()
typedef long long ll;
typedef pair<int,int> pii;
const int INF = 1<<29;
const double PI = acos(-1);
const double EPS = 1e-8;

int n;

int solve(const string &s, int b, int g, int now, bool f) {
  if (b == 0 || g == 0) {
    return b;
  }
  int m = s.size();
  int res = 0;
  if (!f) {                     // 0-origin
    int p = (now + n) % m;
    string t = s.substr(0,p);
    if (p<m-1) t += s.substr(p+1);
    int tb = b, tg = g;
    if (s[p] == 'B') tb--; else tg--;
    res = max(res, solve(t,tb,tg,p,1));
  }
  // 1-origin
  int p = (now + n - 1) % m;
  string t = s.substr(0,p);
  if (p<m-1) t += s.substr(p+1);
  if (s[p] == 'B') b--; else g--;
  res = max(res, solve(t,b,g,p,f));
  return res;
}
int main() {
  int T;
  cin >> T;
  while(T--) {
    cin >> n;
    string s;
    cin >> s;
    int b = 0, g = 0; 
    REP(i,s.size()) {
      if (s[i] == 'B') b++;
      else g++;
    }
    cout << solve(s, b, g, 0, 0) << endl;
  }
}