#include <bits/stdc++.h>
using namespace std;
int main() {
  int t;
  cin >> t;
  while (t--) {
    int n;
    string s;
    cin >> n >> s;
    int count = 0;
    bool countA = false;
    for (int i = 0; i < s.length(); i++) {
      if (s[i] == 'A') {
        countA = true;
        break;
      }
    }
    if (s.length() == 1 && s[0] == 'P')
      cout << "0" << endl;
    else {
      while (true && countA) {
        int flag = 0;
        for (int i = 0; i < s.length() - 1; i++) {
          if (s[i] == 'A' && s[i + 1] != 'A') {
            flag = 1;
            s[i + 1] = 'A';
            i++;
          }
        }
        if (flag == 1)
          count++;
        else if (flag == 0)
          break;
      }
      cout << count << endl;
    }
  }
  return 0;
}
