#include <bits/stdc++.h>
using namespace std;
long long a, b, c, n, ans, cn1, cn2, cn3, t;
string s, s1, s2;
int main() {
  ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
  cin >> t;
  while (t--) {
    cin >> n;
    cin >> s;
    a = 0;
    cn1 = 0;
    bool haha = false;
    for (int i = 0; i < n; i++) {
      if (s[i] == 'A') {
        a = i;
        haha = true;
        break;
      }
    }
    ans = 0;
    if (haha) {
      for (int j = a + 1; j < n; j++) {
        if (s[j] == 'P')
          ++cn1;
        else
          cn1 = 0;
        ans = max(cn1, ans);
      }
    }
    cout << ans << endl;
  }
  cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << "s.\n";
  return 0;
}
