#include<iostream>
using namespace std;
int main()
{
int num,times;
while(cin>>num>>times)
{
for(int i=0;i<times;i++)
{
int guess;
cin>>guess;
int g[4],hold1=guess;
for(int j=3;j>=0;j--)
{
g[j]=hold1%10;
hold1=int(hold1/10);
}
int n[4],hold2=num;
for(int j=3;j>=0;j--)
{
n[j]=hold2%10;
hold2=int(hold2/10);
}
int a=0,b=0;
for(int j=0;j<4;j++)
{
if(n[j]==g[j])
a++;
else
{
for(int k=0;k<4;k++)
{
if(g[j]==n[k]&&j!=k)
b++;
}
}
}
cout<<a<<"A"<<b<<"B"<<endl;
}
}
return 0;
}