#include <bits/stdc++.h>
using namespace std;
int tem;
int main() {
  int t;
  cin >> t;
  while (t--) {
    int n;
    cin >> n;
    string s;
    cin >> s;
    int i = 0;
    while (i < n && s[i] != 'A') i++;
    if (i == n) {
      printf("0\n");
      continue;
    }
    int res = 0;
    while (i < n) {
      while (i < n && s[i] != 'P') i++;
      int cou = 0;
      while (i < n && s[i] == 'P') i++, cou++;
      res = max(res, cou);
    }
    cout << res << endl;
  }
  return 0;
}
