#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  while (n--) {
    int x, flag = 0, c1 = 0, c2 = 0;
    cin >> x;
    string str;
    cin >> str;
    for (int i = 0; i < str.length(); i++) {
      if (str[i] == 'A') {
        flag = 1;
        c1 = 0;
      }
      if (str[i] == 'P' && flag == 1) c1++;
      if (c1 > c2) c2 = c1;
    }
    cout << c2 << endl;
  }
  return 0;
}
