#include <bits/stdc++.h>
using namespace std;
string s;
bool finished() {
  for (int i = 0; i < s.length() - 1; i++)
    if (s[i] == 'A' && s[i + 1] == 'P') return false;
  return true;
}
int main() {
  int N;
  cin >> N;
  for (int n = 0; n < N; n++) {
    int x;
    cin >> x;
    cin >> s;
    int res = 0;
    while (!finished()) {
      res++;
      for (int i = 0; i < x - 1; i++)
        if (s[i] == 'A' && s[i + 1] == 'P') {
          s[i + 1] = 'A';
          i++;
        }
    }
    cout << res << endl;
  }
  return 0;
}
