#include <bits/stdc++.h>
using namespace std;
int main() {
  long long int a, b, t, i, j, n, cnt, cnt1, p;
  string s;
  vector<long long int> v;
  cin >> t;
  while (t--) {
    cnt = cnt1 = 0;
    cin >> n >> s;
    p = s.length();
    for (i = 0; i < n; i++) {
      if (s[i] == 'A') {
        p = i;
        break;
      }
    }
    for (j = p; j < n; j++) {
      if (s[j] == 'P') {
        cnt++;
        continue;
      }
      if (cnt >= cnt1) {
        cnt1 = cnt;
      }
      cnt = 0;
    }
    if (cnt >= cnt1) {
      cnt1 = cnt;
    }
    cout << cnt1 << "\n";
  }
  return 0;
}
