#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std; const int maxn=+;
char s[maxn]; int main()
{
int T;
scanf("%d",&T);
while(T--)
{
scanf("%s",s);
int len=strlen(s);
int ans=;
for(int i=;s[i];i++)
{
if(i<len&&i+<len&&i+<len&&s[i]=='Q'&&s[i+]=='A'&&s[i+]=='Q')
ans++;
}
printf("%d\n",ans); }
return ;
}