#include <bits/stdc++.h>
using namespace std;
long long N = 500005;
long long MAX = 9223372036854775807;
long long MOD = 1000000007;
long double PI = 4 * atan(1);
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  ;
  cout.precision(30);
  int t;
  cin >> t;
  int n;
  string second;
  while (t--) {
    cin >> n;
    cin >> second;
    long long ans = 0, nbr = 0;
    bool res = false;
    for (int i = 0; i < n; i++) {
      nbr = 0;
      res = false;
      if (second[i] == 'A') {
        while (i < n && second[i] == 'A') {
          i++;
          res = true;
        }
        while (i < n && second[i] == 'P') {
          i++;
          nbr++;
          res = true;
        }
        ans = max(ans, nbr);
      }
      if (res) i--;
    }
    cout << ans << "\n";
  }
  return 0;
}
