A. Safe

The WSA prints every important document and stores the printouts in large, secure safes. As per policy, safes always must be closed after use, forcing employees to open each safe hundreds of times per day. A key point of optimizing processes at the WSA is to speed up entering passwords for safes.

The keyboard keys on the safe are not ordinary keys, every time they are pressed they don't just enter the letter displayed on them as input, but the letter displayed on them changes as well. For each key, there is a cycle of letters that can appear

The cycle of each key (actual letters the key would input push-by-push, in order) is known as well as the password that needs to be entered. The code is accepted only if a password of length N is entered using the last N key presses (that is, there's no way to reset cycles in between). All keys start on the first letter of their cycle (and the cycle can loop around).

   letter code disk
source: http://commons.wikimedia.org/wiki/File:Enigma_rotors_with_alphabet_rings.jpg

The task is to find the least amount of key presses needed to enter the code.

For each keyboard there are several different passwords that the task should be solved for.


The first line contains one number, N, the number of keys. The next N lines contain the cycle for each key, consisting only of capital English letters. The following line contains M, the number of passwords that need to be typed on this keyboard. The next M lines contain the M passwords.


A single number on a separate line for each input code, in order. If it is possible to enter the code, output the number of key presses necessary, otherwise output "-1".

Example input


Example output