#P3955. Library Book Code Matching

    ID: 17203 Type: Default 1000ms 256MiB

Library Book Code Matching

Library Book Code Matching

In the library each book has a unique book code (a positive integer) used for fast retrieval. Each borrower holds a demand code (also a positive integer). A book is considered as the required book for a reader if the book code ends with the reader's demand code. In other words, if we let a book code be represented as an integer B and a demand code as an integer D, then the book qualifies if and only if its string representation ends with that of D. Equivalently, if k is the number of digits of D, then the book qualifies if \[ B \mod 10^{k} = D \] for some k (where the modulus operation is taken in base 10).

Your task is to help the library administrator by processing queries from different readers. For each reader, output the minimal book code (in value) among those that satisfy the condition. If no such book exists, output -1.

inputFormat

The input consists of the following:

  1. The first line contains two integers n and m (the number of books and the number of readers, respectively).
  2. The second line contains n positive integers representing the book codes.
  3. The following m lines each contain a positive integer representing a reader's demand code.

Note: All numbers are separated by spaces or newline characters.

outputFormat

For each reader, output on a new line the minimal book code that ends with the given demand code. If no such book exists, output -1.

sample

5 3
12345 234 34567 145 345
45
234
7
145

234 -1

</p>