Deciphering

Time limit2sMemory limit64 MB

Summary
Count the ways to split an unspaced text into dictionary words, group them into sentences, and match each sentence to a valid part-of-speech rule, capping the huge counts.
Level

Medium7 of 10

Topics
Dynamic programming, String, Trie, Combinatorics
Solved
No attempts yet

Problem

The archaeologist Mr. Ford Trunkings has uncovered the ruins of the ancient Velulu tribe deep in Africa. The Velulu were remarkably advanced for their age and even had an alphabet of their own, but a glacial period roughly 20,000 years ago destroyed their civilization and their writings were almost entirely lost.

Your job is to help decipher the surviving Velulu texts. The trouble is that Velulu writing uses no spaces at all: every text is a single unbroken string of letters, with all of its words run together.

The researchers have already compiled a draft dictionary of the language. Splitting a text into dictionary words alone, however, yields an enormous number of possibilities for almost any text of reasonable length. To narrow things down they also reconstructed a set of sentence construction rules, each describing the order in which parts of speech may appear within a single sentence.

Using the dictionary together with these rules, determine in how many ways the given text can be parsed.

Input

The first line contains three integers nn, mm and kk (1≤n≤50001 \le n \le 5000, 1≤m≤101 \le m \le 10, 1≤k≤101 \le k \le 10): the number of dictionary words, the number of sentence construction rules, and the number of distinct parts of speech.

Each of the next nn lines describes one word. The line starts with the word itself — a non-empty string of fewer than 20 lowercase English letters — followed by an integer kik_i (1≤ki≤101 \le k_i \le 10) and then kik_i integers ai1<ai2<⋯<aikia_{i1} < a_{i2} < \dots < a_{i k_i} (1≤aij≤k1 \le a_{ij} \le k) in strictly increasing order, the parts of speech this word can stand for. Each word appears exactly once, and the words are listed in arbitrary order.

Each of the next mm lines describes one construction rule. The line starts with an integer lil_i (1≤li≤101 \le l_i \le 10), the number of words in this kind of sentence, followed by lil_i integers bi1,bi2,…,bilib_{i1}, b_{i2}, \dots, b_{i l_i} (1≤bij≤k1 \le b_{ij} \le k): the parts of speech required at each position, in order. No rule is repeated.

The last line contains the text to decipher: a non-empty string of fewer than 1000 lowercase English letters.

Output

Print a single line with the number of distinct parsings of the text. If that number is greater than 101810^{18}, print TOO MANY instead. If the text cannot be parsed at all, print 0.

A parsing consists of:

  1. a split of the text into a sequence of words, each of which appears in the dictionary;
  2. a grouping of those words, keeping their order, into one or more consecutive sentences; and
  3. for every sentence, a chosen construction rule whose list of parts of speech is exactly as long as the sentence and such that, at every position, the word standing there can act as the part of speech the rule requires.

Two parsings are considered different if they differ in the word split, in the grouping into sentences, or in the rule chosen for any sentence. (In particular, one and the same sequence of words is counted once for each construction rule it satisfies.)

Examples6

  1. Example 1

    Input
    5 2 2
    ba 1 2
    za 2 1 2
    a 2 1 2
    caba 1 1
    ab 1 1
    2 1 2
    3 2 2 1
    abazabacaba
    
    Expected output
    2
    
  2. Example 2

    Input
    1 1 3
    q 1 3
    1 3
    q
    
    Expected output
    1
    
  3. Example 3

    Input
    1 2 2
    a 2 1 2
    1 1
    1 2
    a
    
    Expected output
    2
    
  4. Example 4

    Input
    1 1 1
    ab 1 1
    1 1
    abc
    
    Expected output
    0
    
  5. Example 5

    Input
    3 2 2
    a 2 1 2
    b 2 1 2
    ab 1 1
    1 1
    2 1 2
    abab
    
    Expected output
    11
    
  6. Example 6

    Input
    6 3 3
    x 1 1
    y 1 2
    z 1 3
    xy 2 1 2
    yz 2 2 3
    xyz 3 1 2 3
    2 1 2
    2 2 3
    3 1 2 3
    xyzxyz
    
    Expected output
    13