def main():
from sys import stdin
s = stdin.readline().strip()
cs = {it:0 for it in s}
for it in s:
cs[it] += 1
p = int(stdin.readline())
for _ in range(p):
if _ > 0: print()
# 先把某玩家猜的字收集起來
t = stdin.readline().rstrip()
g = []
while t != "#":
g.append(t)
t = stdin.readline().rstrip()
num = 0
for guess in g:
if len(guess) > 5: print("Too long");continue
if len(guess) < 5: print("Not enough letters");continue
num += 1
# 猜對就不用再處理了
if guess == s:
print("OOOOO")
print(f'Congradulat1ons ! You guessed {num} times')
break
ans = [None for _ in range(5)]
ct = cs.copy()
not_ok = []
for i in range(5):
if guess[i] == s[i]:ans[i] = "O";ct[guess[i]] -= 1;continue
# 把還需要處理的 index 撿起來
if guess[i] in cs:not_ok.append(i)
else:ans[i] = "X"
for i in not_ok:
if ct[guess[i]]: ans[i] = "Y";ct[guess[i]] -= 1
else:ans[i] = "X"
print('{}{}{}{}{}'.format(ans[0],ans[1],ans[2],ans[3],ans[4]))
main()