Longest Common Prefix
时间限制:C/C++/Rust/Pascal 3秒,其他语言6秒
空间限制:C/C++/Rust/Pascal 512 M,其他语言1024 M
64bit IO Format: %lld

题目描述

Given n strings s_1,s_2,\ldots,s_n consisting of \texttt{0}, \texttt{1}, or \texttt{2}, you need to answer q queries. In each query, given a string t and interval endpoints l and r, find the value of the following expression:

\sum_{i=l}^r \mathrm{lcp}(t,s_i)

where \mathrm{lcp}(a,b) denotes the length of the longest common prefix of a and b.

输入描述:

The first line of input contains an integer T (1\le T\le 10^5), denoting the number of test cases.

For each test case, the first line contains two integers n and q (1\le n,q\le 10^5), denoting the number of strings and the number of queries, respectively.

The next n lines each contain a string s_i consisting of \texttt{0}, \texttt{1}, or \texttt{2} (1\le \mid s_i\mid\le 40), as described above.

The next q lines each describe a query. Each line contains two integers l and r (1\le l\le r\le n) and a string t consisting of \texttt{0}, \texttt{1}, or \texttt{2} (1\le \mid t\mid\le 40), representing the left and right boundaries of the interval to be summed and the query string.

It is guaranteed that the input satisfies \sum n\le 10^5 and \sum m\le 10^5.

输出描述:

For each query, output one line containing an integer representing the answer to the query.
示例1

输入

复制
1
4 3
0112
01201
01121
0012
1 3 0112
2 4 11121
3 4 0

输出

复制
10
0
2