#include <bits/stdc++.h>
const long long INF = 1e18;
using namespace std;
long long POWER(long long base, long long expo) {
  long long num = 1;
  long long i = expo;
  while (i) {
    num = num * base;
    i--;
  }
  return num;
}
int main() {
  long long t = 1;
  cin >> t;
  while (t--) {
    long long n;
    cin >> n;
    string s;
    cin >> s;
    long long time = 0, start = -1, mx = 0;
    for (long long i = 0; i < n; i++) {
      if (s[i] == 'A') {
        mx = max(mx, time);
        time = 0;
        if (start == -1) start = 1;
      } else if (s[i] == 'P' && start == 1)
        time++;
    }
    mx = max(mx, time);
    cout << mx << endl;
  }
  return 0;
}
