#include <bits/stdc++.h>
using namespace std;
int ans = 0;
bool f(vector<int> a, vector<int> b) {
  b[0] = a[0];
  for (int i = 1; i < a.size(); i++) {
    b[i] = min(b[i - 1], a[i]);
  }
  int f = 0;
  for (int i = 0; i < a.size(); i++) {
    if (b[i] < a[i]) {
      f++;
      ans++;
      break;
    }
  }
  if (f != 0)
    return true;
  else
    return false;
}
int main() {
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    int m;
    cin >> m;
    string s;
    cin >> s;
    int cnt = 0, f = 0;
    int mx = 0;
    for (int j = 0; j < m; j++) {
      if (s[j] == 'A') {
        f++;
        mx = max(cnt, mx);
        cnt = 0;
      }
      if (s[j] == 'P' && f != 0) {
        cnt++;
      }
    }
    mx = max(cnt, mx);
    cout << mx << endl;
  }
}
