Under construction

Still being built. Things may change

ZigZag

Interview

Time limit2sMemory limit64 MB

Summary
Given K words and N letters, output for each letter the word starting with it that has been used fewest times, breaking ties alphabetically.
Level

Medium4 of 10

Topics
Sorting, Hash map, Implementation, Greedy
Solved
No attempts yet

Problem

Zig and Zag play a word game. Zig says one lowercase letter, and Zag answers with a word that starts with that letter. The word must come from a fixed word list, and among the words on the list that start with that letter, Zag says the one he has said the fewest times so far. If several words tie, Zag says the one that comes first in alphabetical order. For every letter Zig says, a word is always available.

You are given a list of KK distinct words and the NN letters Zig says. Write a program that prints the NN words Zag says, in order.

Input

The first line contains KK (1≤K≤100 0001 \le K \le 100\,000) and NN (1≤N≤100 0001 \le N \le 100\,000).

Each of the next KK lines contains one word, a string of lowercase English letters of length at most 2121.

Each of the next NN lines contains one lowercase English letter that Zig says.

Output

Print NN lines. Line ii contains the word Zag says in answer to the ii-th letter.

Examples3

  1. Example 1

    Input
    4 5
    zagreb
    split
    zadar
    sisak
    z
    s
    s
    z
    z
    
    Expected output
    zadar
    sisak
    split
    zagreb
    zadar
    
  2. Example 2

    Input
    5 3
    london
    rim
    pariz
    moskva
    sarajevo
    p
    r
    p
    
    Expected output
    pariz
    rim
    pariz
    
  3. Example 3

    Input
    1 3
    zagreb
    z
    z
    z
    
    Expected output
    zagreb
    zagreb
    zagreb