#include <bits/stdc++.h>
using namespace std;
int main() {
  int t;
  cin >> t;
  while (t--) {
    int k;
    string s;
    int ans = 0, tmp = 0;
    bool ok = false;
    cin >> k >> s;
    for (int i = 0; i < k; i++) {
      if (s[i] == 'A') ok = true;
      if (s[i] == 'P' && ok) tmp++;
      if ((s[i] == 'A' && tmp != 0) || i == k - 1) {
        ans = max(ans, tmp);
        tmp = 0;
      }
    }
    cout << ans << endl;
  }
}
