/*
大前提:只出现 P A T 且 P 和 A 只出现一次 且顺序符合 条件2
1. A*PATA* 中间A的个数为1,两边A的个数相等
2. PA+T 中间A的个数大于1,两边A的个数为0
3. A*PA+TA*xA+ 左边A的个数乘以中间A的个数等于右边A的个数
*/
#include<bits/stdc++.h>
using namespace std;
int main() {
string str;
int n;
cin >> n;
while ( n-- ) {
int loc_p = 0, loc_t = 0, count_f = 0, count_m = 0, count_b = 0;
cin >> str;
bool flag = true;
for ( int i = 0; i < str.size(); i++ ) {
if ( str[i] == 'P' || str[i] == 'A' || str[i] == 'T' ) {
if ( loc_p == 0 && str[i] == 'A' ) {
count_f++;
}
else if ( loc_p == 1 && loc_t == 0 && str[i] == 'A' ) {
count_m++;
}
else if ( loc_t == 1 && str[i] == 'A' ) {
count_b++;
}
else if ( str[i] == 'P' ) {
loc_p++;
}
else if ( str[i] == 'T' ) {
loc_t++;
}
}
else {
flag = false;
}
}
if ( !flag ) {
cout << "NO" << endl;
}
else if ( (loc_p == 1 && loc_t == 1 && count_m == 1 && count_f == count_b) ||
(loc_p == 1 && loc_t == 1 && count_m >= 1 && count_f == 0 && 0 == count_b) ||
(loc_p == 1 && loc_t == 1 && count_b == count_f * count_m && count_m >= 1) )
{
cout << "YES" << endl;
}
else
{
cout << "NO" << endl;
}
}
return 0;
}