The Vigenere Cipher is a well known cipher in cryptography. The encryption of a string , with the help of a key-string , both having the same lengths, is done by the following way:
Let be the final string that will be obtained by the encryption of string using the key .
The string will have the same length as the previous strings, and will respect the following property:
where is the ordinal number of the character in the english alphabet (, and so on), and is the function that returns the character with the ordinal number in the english alphabet.
Briefly, the encryption process can be described as an iterative addition, character by character, modulo 26, of the string to the string .
Let's define a modification over the Vigenere Cipher, which we will refer to as: Cyclic Vigenere
, the following way:
The encryption of string with key-string is possible only when the length of string divides the length of string . The result of the encryption is string , defined as:
where denotes the length of the string, is the remainder of by , and the indexing of the strings starts at 0.
Briefly, the encryption process can be described as an iterative addition, character by character, modulo 26, of the repetition of the string to the string .
In this task, key-strings are given, and strings that need to be encrypted. For any string from the strings, print the index of the key-string that generates the minimal possible lexicographic encryption of the string . If there are multiple answers, the one with the smallest index must be printed. If there is no answer, print .
Input data
The first line of input contains and .
The next lines contain the key-strings .
The next lines contain the strings .
Output data
Output lines. The -th of them must contain the index of the key-string that generates the minimal possible lexicographic encryption of the string . The indexes are considered to start from .
If there are multiple answers for any of the strings, print the minimal possible index of a key-string .
If there is no such key-string, print .
Constraints and Clarifications
- .
- .
- The total lengths of all strings and key-strings is at most .
- Each string contains only lowercase English letters.
# | Score | Restrictions |
---|---|---|
1 | 0 | Examples. |
2 | 20 | The total length of all strings and key-strings multiplied by is at most . |
3 | 50 | The length of each key-string divides the length of each string. |
4 | 30 | No additional limitations. |
Example
stdin
3 6
are
mere
ana
oare
cinestie
nimeni
anaana
plebi
nusestie
stdout
3
-1
4
Explanation
In the sample, for the first string anaana
, the key-string are
produces aeeaee
, the key-string ana
produces aaaaaa
, and the key-string nimeni
produces nvmeai
, the lexicographically minimal encryption result is aaaaaa
, which is produced by the third key-string, so the answer is .
For the second string plebi
, there is no suitable key-string, since it has length 5, which is not divisible by any of the lengths of the key-strings, thus the answer is .
For the third string nusestie
, the key-string mere
produces zyjiexzi
, the key-string oare
, produces bujigtzi
, and the key-string cinestie
produces pcfikmqi
, the lexicographically minimal encryption result is bujigtzi
, which is produced by the fourth key-string, so the answer is .