#include <bits/stdc++.h>
using namespace std;
string s;
int T, N;
int main() {
  scanf("%d", &T);
  for (int tc = 1; tc <= T; tc++) {
    scanf("%d", &N);
    cin >> s;
    int cnt = 0;
    bool flag;
    while (1) {
      flag = false;
      for (int i = 0; i < N - 1; i++) {
        if (s[i] == 'A' && s[i + 1] == 'P') {
          s[i + 1] = 'A';
          i++;
          flag = true;
        }
      }
      if (flag == false) break;
      cnt++;
    }
    printf("%d\n", cnt);
  }
  return 0;
}
