real = input()
n = int(input())
for i in range(n):
a = 0
b = 0
c = input()
for j in range(4):
if c[j] == real[0]:
b += 1
if c[j] == real[1]:
b += 1
if c[j] == real[2]:
b += 1
if c[j] == real[3]:
b += 1
if c[j] == real[j]:
a += 1
b -= 1
print(f"{a}A{b}B")