#include <bits/stdc++.h>
using namespace std;
void solve() {
  long long n;
  cin >> n;
  string s;
  cin >> s;
  long long c = 0, a = 0;
  for (long long i = 0; i < s.size(); i++) {
    if (s[i] == 'P') {
      a++;
    }
  }
  if (a == s.size()) {
    cout << "0" << endl;
    return;
  }
  for (long long i = 0; i < s.size(); i++) {
    if (s[i] == 'A') {
      c = i;
      break;
    }
  }
  if (c == s.size() - 1) {
    cout << "0" << endl;
    return;
  }
  long long c1 = 0, mx = 0;
  for (long long i = c; i < s.size(); i++) {
    if (s[i] != 'A') {
      c1++;
    } else {
      mx = max(mx, c1);
      c1 = 0;
    }
  }
  mx = max(mx, c1);
  cout << mx << endl;
}
signed main() {
  long long t = 1;
  cin >> t;
  while (t--) {
    solve();
  }
}
