#include <bits/stdc++.h>
using namespace std;
int main() {
  int t;
  cin >> t;
  while (t--) {
    int n;
    string s;
    cin >> n >> s;
    int ans = 0, ch = 1;
    vector<int> v;
    for (int i = 0; i < n; i++) {
      if (s[i] == 'A') v.push_back(i);
    }
    while (!v.empty()) {
      vector<int> vv;
      for (int i = 0; i < v.size(); i++) {
        int id = v[i];
        if ((id < n - 1) && (s[id] == 'A') && (s[id + 1] == 'P')) {
          s[id + 1] = 'A';
          vv.push_back(id + 1);
        }
      }
      v = vv;
      if (!v.empty()) ans++;
    }
    cout << ans << "\n";
  }
  return 0;
}
