Under construction

Still being built. Things may change

Clever Title

Time limit5sMemory limit512 MB

Summary
For each uppercase word, count how many orderings of the n author names let you pick one uppercase letter from each name, left to right, to spell the word.
Level

Medium4 of 10

Topics
Backtracking, Brute force, String, Combinatorics
Solved
No attempts yet

Problem

One day I sat down in my chair and wondered which theorem is the best one. After an hour on my favourite search engine (Ask Jeeves) I found the BEST Theorem. (The BEST Theorem gives a formula for the number of Eulerian circuits in a directed graph, which this problem does not need.) The name comes from the uppercase letters of the authors of the paper: de Bruijn, van Aardenne-Ehrenfest, Smith and Tutte.

A title is clever when you can line the authors up in any order you like, pick one uppercase letter from each author's name, and read the picked letters from left to right to get the title. The title therefore has as many letters as there are authors.

Last year the problem setters of the South Pacific Region proved a theorem about the number of powers modulo a fixed number, and they would like to give it a clever title. Their names are Anderson, Best, Cameron-Jones, Corney, French, Guttmann, Haigh, Lobb, Sergeev, Tran, Ward-Graham and Whalan. The ABCCFGHLSTWW Theorem does take one uppercase letter from each name, but ABCCFGHLSTWW is not a valid word. (In the real world a valid word is an English word. In this problem the list of valid words is part of the input.) No ordering of those names spells a valid word at all. If only Anderson, French, Sergeev and Tran had discovered a theorem, they could call it the FAST Theorem (French, Anderson, Sergeev, Tran), and that is the only ordering of those four names that spells FAST. If the authors were Merlin, Oberon and Othello and they wanted the MOO Theorem, two orderings work: Merlin, Oberon, Othello and Merlin, Othello, Oberon.

The order of the names on a publication usually matters. I already have tenure, so I only care about how many orderings make a title clever. You are given the authors of a theorem and a list of valid titles. For each title, count the orderings of the author names that make it clever.

Input

The first line contains two integers nn (1≤n≤101 \le n \le 10), the number of authors, and kk (1≤k≤10001 \le k \le 1000), the number of valid words.

Each of the next nn lines contains one author name. A name uses only uppercase and lowercase English letters, has between 1 and 20 characters, and contains at least one uppercase letter.

Each of the next kk lines contains one valid word. A valid word uses only uppercase English letters and has between 1 and 20 characters.

Output

Print kk integers, one per line. Line ii holds the number of orderings of the author names that make the ii-th given word a clever title.

Examples4

  1. Example 1

    Input
    4 1
    deBruijn
    vanAardenneEhrenfest
    Smith
    Tutte
    BEST
    
    Expected output
    1
    
  2. Example 2

    Input
    10 3
    Anderson
    Best
    CameronJones
    Corney
    French
    Guttmann
    Haigh
    Lobb
    Sergeev
    Tran
    DOESTHISWORK
    TOOSHORT
    THISISTOOLONG
    
    Expected output
    0
    0
    0
    
  3. Example 3

    Input
    4 2
    Anderson
    French
    Sergeev
    Tran
    SLOW
    FAST
    
    Expected output
    0
    1
    
  4. Example 4

    Input
    3 1
    AA
    BB
    AB
    AAB
    
    Expected output
    2