ans = input()
gtimes = int(input())
for i in range(gtimes):
a = 0
b = 0
gans = input()
for j in range(4):
if ans[j] == gans[j]:
a+=1
elif gans[j] in ans:
b+=1
print(str(a)+'A'+str(b)+'B')