#include<bits/stdc++.h>
using namespace std;
long long t,ac,pc,cc,cnt=0;
char s[300001]={};
long long a[300001]={},p[300001]={},c[300001]={};
int main()
{
cin>>t;
while (t--)
{
ac=0;pc=0;cc=0;cnt=0;
cin>>s;
for (int i=0;i<strlen(s);i++)
{
if (s[i]=='A'){
a[++ac]=i+1;
}
if (s[i]=='P'&&ac>pc){
p[++pc]=i+1;
}
if (s[i]=='C'&&pc>cc){
cnt++;
s[i]=' ';
s[a[cnt]-1]=' ';
s[p[cnt]-1]=' ';
c[++cc]=i+1;
}
}
if (3*cnt!=strlen(s)){
for (int i=0;i<strlen(s);i++)
{
if (s[i]!=' ')
cout<<s[i];
}
}
else{
cout<<"Perfect";
}
cout<<endl<<cnt<<endl;
for (int i=1;i<=cnt;i++)
{
cout<<a[i]<<" "<<p[i]<<" "<<c[i]<<endl;
}
}
return 0;
}