# Arup Guha
# 3/8/2025
# Solution to Kattis Problem: Nicknames

# Get number of names.
n = int(input())

# We are creating 10 maps (I am using indexes 1 to 10.) One for
# each length of name.
mymaps = []
for i in range(11):
    mymaps.append({})

# Loop through the names.
for i in range(n):

    # Get the name.
    s = input().strip()

    # Loop through all substrings of the name so s[0..j-1]
    # Update each of these maps accordingly.
    for j in range(1,len(s)+1):

        # Here is the substring.
        tmp = s[:j]

        # If it's in the map, add 1 more to this count.
        if tmp in mymaps[j]:
            mymaps[j][tmp] += 1

        # Add a new entry.
        else:
            mymaps[j][tmp] = 1

# Process queries.
numQ = int(input())
for i in range(numQ):

    s = input().strip()

    # We have this one, look in the map.
    if s in mymaps[len(s)]:
        print(mymaps[len(s)][s])

    # Not in the map, so answer is 0.
    else:
        print(0)
