num = input().replace(""," ")
num = num.split()
n = int(input())
for i in range(n):
a = 0
b = 0
num_guess = input().replace(""," ")
num_guess = num_guess.split()
for j in range(4):
if num_guess[j] == num[0]:
b += 1
if num_guess[j] == num[1]:
b += 1
if num_guess[j] == num[2]:
b += 1
if num_guess[j] == num[3]:
b += 1
if num_guess[j] == num[j]:
a += 1
b -= 1
print(f"{a}A{b}B")