#include <bits/stdc++.h>
#pragma GCC optimize(2)
#pragma warning(disable : 4996)
using namespace std;
const long long INF = 2147483647;
const double PI = acos(-1);
const int mod = 1e9 + 7;
char arr[121212];
int main() {
  long long t;
  cin >> t;
  for (int j = 1; j <= t; j++) {
    long long n;
    cin >> n;
    cin >> (arr + 1);
    long long ans = 0;
    bool sign = false;
    while (sign == false) {
      sign = true;
      for (int i = 1; i <= n - 1; i++) {
        if (arr[i] == 'A' && arr[i + 1] == 'P') {
          arr[i + 1] = 'A';
          sign = false;
          i = i + 1;
        }
      }
      if (!sign) ans++;
    }
    cout << ans << endl;
  }
  return 0;
}
