logo AlgoBeat OnlineJudge
登录 注册

#215781. [JAG 2023 Summer Camp #3] LCP Queries

内存限制:1024 MiB 时间限制:2500 ms 标准输入输出
题目类型:VJudge(洛谷) 评测方式:VJudge
上传者: 匿名

题目描述

A string is called a prefix of a string if can be obtained by repeating the removal of the last letter of zero or more times. For example, “abac”, “aba”, “ab”, “a”, and an empty string are the prefixes of “abac”.

For two strings and , let be the length of the longest common prefix of and . For example, because the longest common prefix of these two strings is “abac”. Note that is always defined for any strings and because at least an empty string is one of their common prefixes.

You are given strings and strings of lowercase English letters. Then, you are given queries. In each query you are given an integer sequence . Let be the concatenation of . Your task is to calculate .

输入格式

The input consists of a single test case of the following format.

The first line consists of an integer . Each of the next lines consists of a non-empty string of lowercase English letters. The next line consists of an integer . Each of the next lines consists of a non-empty string of lowercase English letters.

The next line consists of an integer . Then queries are given in order. Each of the queries is given in a single line in the following format.

is a positive integer which represents the length of the integer sequence of this query. Each is an integer between and , inclusive.

You can assume that , and . The sum of lengths of does not exceed . Similarly, the sum of lengths of does not exceed . The sum of over all queries does not exceed .

输出格式

Output lines. The -th line should be the answer to the -th query.

样例

样例输入 1

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

样例输出 1

4
9
3
1
0