#include<iostream>
#include<string>
#include<stdio.h>
using namespace std;
bool judge(string s)
{
bool cnta = 0, cntb = 0, cntc = 0;
for (int i = 0; i < s.length(); i++)
{
if (s[i] == 'P')cnta = 1;
else
if (s[i] == 'A')cntb = 1;
else
if (s[i] == 'T')cntc = 1;
else
return false;
}
if (cnta&&cntb&&cntc)
{
int posP=0, posT=0;
for (int i = 0; i < s.length(); i++)
{
if (s[i] == 'P')
{
posP = i;
break;
}
}
for (int i = 0; i < s.length(); i++)
{
if (s[i] == 'T')
{
posT = i;
break;
}
}
for (int i=posP+1; i < posT; i++)
{
if (s[i] != 'A')
return false;
}
int countP = posP;
int countA_mid = posT - posP - 1;
int countA_last = s.length() - posT - 1;
if (countP * countA_mid== countA_last)
return true;
else
return false;
}
return false;
}
int main()
{
int n;
cin >> n;
string s;
for (int i = 0; i < n; i++)
{
cin >> s;
if (judge(s))
{
cout << "YES" << endl;
}
else
{
cout << "NO" << endl;
}
}
system("pause");
return 0;
}