#include<iostream>
#include<algorithm>
#include<stdio.h>
#include<string.h>
#include<map>
#include<string>
using namespace std;
int main() {
#ifdef LOCAL
freopen("d:\\datain.txt", "r", stdin);
freopen("d:\\dataout.txt", "w", stdout);
#endif LOCAL
int n;
cin >> n;
for (int i = 0; i < n; i++) {
bool res = false;
char s[101];
scanf("%s", s);
//cout << s << endl;
int n1 = strlen(s);
int p = 0;
int t = 0;
int np = 0;
int nt = 0;
int na = 0;
for (int j = 0; j < n1; j++) {
if (s[j] == 'P') {
np++;
p = j;
}
else if (s[j] == 'T') {
nt++;
t = j;
}
else if (s[j] == 'A') {
na++;
}
}
if (np == 1 && nt == 1 && na == n1-2&&na>0) {
if (t - p == 2 && n1 - t-1 == p) {
res = true;
}
else if (p*(t - p-1) == n1 - t-1) {
res = true;
}
}
//cout << "n1:"<<n1<<" P:" << p << " t:" << t << endl;
//cout << " np:" << np << " nt:" << nt << " na:" << na << endl;
if (res == true) {
printf("YES");
}
else
{
printf("NO");
}
cout << endl;
}
return 0;
}