#include <bits/stdc++.h>
using namespace std;
int n, m;
string s;
int main() {
  cin >> n;
  for (int i = 1; i <= n; ++i) {
    cin >> m >> s;
    int mx = -1;
    for (int j = 0; j < m - 1; ++j) {
      if (s[j] == 'A') {
        int c = 0;
        for (int y = j + 1; y < m; ++y) {
          if (s[y] == 'A') {
            break;
          }
          if (s[y] == 'P') {
            c++;
          }
        }
        mx = max(c, mx);
      }
    }
    if (mx == -1) {
      cout << 0 << "\n";
      continue;
    }
    cout << mx << "\n";
  }
  return 0;
}
