#include <bits/stdc++.h>
using namespace std;
int main() {
  int t;
  cin >> t;
  while (t--) {
    int n;
    cin >> n;
    string s;
    cin >> s;
    int i, j = 0, ans = 0;
    for (i = 0; i < n; i++) {
      if (s[i] == 'A' && s[i + 1] == 'P') {
        j = 0;
        while (s[i + 1] == 'P') {
          i++;
          j++;
        }
      }
      ans = max(ans, j);
    }
    cout << ans << endl;
  }
  return 0;
}
