Under construction

Still being built. Things may change

Work-Study Student

Interview

Time limit1sMemory limit1024 MB

Summary
For each sentence, output the meanings of dictionary words that start at each position, using the dictionary-smallest word on ties, or -1 if nothing matches.
Level

Easy3 of 10

Topics
Hash map, String, Implementation
Solved
No attempts yet

Problem

Haru became a work-study student whose job was to build a program that finds English words in a sentence and reports their meanings.

Haru never got around to writing the program. One day before the deadline, Haru asked you to help quickly. Please help Haru.

  • Each entry is a pair (Qi,Ai)(Q_i, A_i), where QiQ_i is an English word and AiA_i is its meaning.
  • Given NN entries and MM sentences, the program answers each sentence as follows.
    • Let the leftmost character of sentence SS be at position 11, and the rightmost at position ∣S∣|S|. Here ∣S∣|S| is the length of SS.
    • The program reads each sentence from the first character to the last, in the order S1,S2,⋯ ,S∣S∣S_1, S_2, \cdots, S_{|S|}.
    • While reading, if the characters at positions k,k+1,⋯ ,k+∣Qi∣−1k, k+1, \cdots, k+|Q_i|-1, joined in order, can form QiQ_i starting at position kk, the program must answer with AiA_i. If several QiQ_i can be formed at position kk, the program answers with the AiA_i of the QiQ_i that comes first in dictionary order.
    • One sentence may require several AiA_i answers.

For example, with the entries (ABC, X), (A, Y), and (CDE, Z) and the sentence ABCDE, the program must answer YXZ.

Input

The first line contains two integers NN and MM, the number of entries and the number of sentences, separated by a space. (1≤N≤1 0001 \leq N \leq 1\ 000; 1≤M≤101 \leq M \leq 10)

From the second line, the next NN lines each contain QiQ_i and AiA_i, separated by a space. (1≤∣Qi∣,∣Ai∣≤101 \leq |Q_i|, |A_i| \leq 10; if i≠ji \neq j, then Qi≠QjQ_i \neq Q_j)

The following MM lines each contain a string SiS_i. (1≤∣S∣≤1001 \leq |S| \leq 100)

All input strings consist only of uppercase English letters and contain no spaces.

Output

For each string SiS_i, print the string that the program must answer, one per line.

If the program has no answer for a string, print -1 instead.

Examples2

  1. Example 1

    Input
    3 1
    ABC X
    A Y
    CDE Z
    ABCDE
    
    Expected output
    YXZ
    
  2. Example 2

    Input
    3 3
    KPSC WELCOME
    KOOK HELLOWORLD
    KOOKMIN FIGHTING
    KPSCWELCOME
    CONTEXT
    KOOKMINUNIVERSITY
    
    Expected output
    WELCOME
    -1
    HELLOWORLDFIGHTING