Under construction

Still being built. Things may change

LCP Queries

Time limit2.5sMemory limit1024 MB

Summary
Given strings s_i and t_j, each query concatenates the listed t_j and asks for the sum of its LCP with every s_i.
Level

Medium7 of 10

Topics
Trie, String matching, Hash map
Solved
No attempts yet

Statement

A string xx is called a prefix of a string yy if xx can be obtained by repeatedly removing the last letter of yy zero or more times. For example, abac, aba, ab, a, and the empty string are the prefixes of abac.

For two strings xx and yy, let LCP(x,y)\text{LCP}(x, y) be the length of their longest common prefix. For example, the longest common prefix of abacab and abacbba is abac, so LCP\text{LCP} of the two is 44. Every pair of strings has the empty string as a common prefix, so LCP(x,y)\text{LCP}(x, y) is always defined.

You are given nn strings s1,…,sns_1, \dots, s_n and mm strings t1,…,tmt_1, \dots, t_m, all made of lowercase English letters. Then you are given qq queries. Each query is an integer sequence a1,…,aka_1, \dots, a_k. Let uu be the concatenation of ta1,…,takt_{a_1}, \dots, t_{a_k}. For each query, compute ∑i=1nLCP(u,si)\sum_{i=1}^{n}{\text{LCP}(u, s_i)}.

Input

The input is a single test case in the following format.

nn

s1s_1

⋮\vdots

sns_n

mm

t1t_1

⋮\vdots

tmt_m

qq

query1\text{query}_1

⋮\vdots

queryq\text{query}_q

The first line has an integer nn. Each of the next nn lines has a non-empty string sis_i. The next line has an integer mm. Each of the next mm lines has a non-empty string tjt_j. The next line has an integer qq. Then qq queries follow, one per line, in this format.

kk a1a_1 ⋯\cdots aka_k

kk is a positive integer, the length of the integer sequence. Each aia_i is an integer between 11 and mm, inclusive.

The constraints are 1≤n≤200 0001 \le n \le 200\,000, 1≤m≤200 0001 \le m \le 200\,000, and 1≤q≤200 0001 \le q \le 200\,000. The sum of lengths of the sis_i is at most 200 000200\,000. The sum of lengths of the tit_i is at most 200 000200\,000. The sum of kk over all queries is at most 200 000200\,000.

Output

Output qq lines. The ii-th line contains the answer to the ii-th query.

Examples1

  1. Example 1

    Input
    5
    abcde
    aaa
    a
    ab
    bcd
    5
    a
    bc
    de
    aaaa
    b
    5
    1 1
    3 1 2 3
    2 2 3
    5 5 4 3 2 1
    3 3 3 3
    
    Expected output
    4
    9
    3
    1
    0