string c;
int a;
cin >> c >> a;
for(int b=0;b<a;b++){
int counta=0,countb=0;
string d;
cin >> d;
for(int A=0;A<4;A++){
for(int B=0;B<4;B++){
if((d[A]==c[B])&&(A==B)){
counta ++;}
else if((d[A]==c[B])&&(A!=B)){
countb ++;}}}
cout<<counta<<"A"<<countb<<"B"<<endl;}}