#include <bits/stdc++.h>
using namespace std;
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  int64_t T;
  cin >> T;
  while (T--) {
    string S;
    int64_t n;
    cin >> n;
    cin >> S;
    S += "A";
    n++;
    int64_t res = 0, tmp = 0;
    bool haveAfirst = false;
    for (int i = 0; i < n; i++) {
      if (S[i] == 'A') haveAfirst = true;
      if (haveAfirst && S[i] == 'P')
        tmp++;
      else if (tmp > 0) {
        res = max(res, tmp);
        tmp = 0;
      }
    }
    cout << res << "\n";
  }
  return 0;
}
