# Arup Guha
# 9/27/24
# Solution to 2024 UCF Qualifier Problem: Injured Shoulder


nWords = int(input())
words = set()

# Create a set of words.
for i in range(nWords):
    word = input().strip()
    words.add(word)

# Here we store in our set all concatenation of 2 words.
doubles = set()
for x in words:
    for y in words:
        doubles.add(x+y)

# Process cases.
n = int(input())
for i in range(n):

    # Get query.
    typed = input().strip()

    # Output accordingly.
    if typed in words:
        print(1)
    elif typed in doubles:
        print(2)
    else:
        print(0)
