#include <bits/stdc++.h>
using namespace std;
int main() {
  int t = 0, k = 0;
  string s;
  cin >> t;
  for (int i = 0; i < t; ++i) {
    cin >> k >> s;
    int res = 0;
    if (s.find('A') == k - 1) {
      cout << res << endl;
      continue;
    }
    while (true) {
      int counter = 0;
      for (int j = 0; j < k - 1; ++j) {
        if (s[j] == 'A' && s[j + 1] == 'P') {
          s[j + 1] = 'A';
          ++j;
          ++counter;
        }
      }
      if (counter == 0) break;
      ++res;
      if (s.find('P') == s.npos) break;
    }
    cout << res << endl;
  }
}
