#include<iostream>
using namespace std;
int main() {
string input, check;
int n, count_B = 0, count_A = 0;
cin >> input >> n;
for (int i = 0; i < n; i++) {
cin >> check;
for (int i = 0; i < 4; i++) {
if (check.at(i) == input.at(i))
count_A++;
else {
for (int j = 0; j < 4; j++) {
if (check.at(i) == input.at(j))
count_B++;
}
}
}
cout << count_A << "A" << count_B << "B" << endl;
count_A = 0, count_B = 0;
}
return 0;
}