Codeforces Round 349 (Div. 1)
E
Forensic Examination
Codeforces
3100
原题
← D→
翻译中…

The country of Reberland is the archenemy of Berland. Recently the authorities of Berland arrested a Reberlandian spy who tried to bring the leaflets intended for agitational propaganda to Berland illegally . The most leaflets contain substrings of the Absolutely Inadmissible Swearword and maybe even the whole word.

翻译中…

Berland legal system uses the difficult algorithm in order to determine the guilt of the spy. The main part of this algorithm is the following procedure.

翻译中…

All the $*m*$ leaflets that are brought by the spy are numbered from $1$ to $*m*$. After that it's needed to get the answer to $*q*$ queries of the following kind: "In which leaflet in the segment of numbers $[*l*, *r*]$ the substring of the Absolutely Inadmissible Swearword $[*p*_{*l*}, *p*_{*r*}]$ occurs more often?".

翻译中…

The expert wants you to automate that procedure because this time texts of leaflets are too long. Help him!

翻译中…

Input

翻译中…

The first line contains the string $*s*$ ($1 ≤ |*s*| ≤ 5·10^{5}$) — the Absolutely Inadmissible Swearword. The string $*s*$ consists of only lowercase English letters.

翻译中…

The second line contains the only integer $*m*$ ($1 ≤ *m* ≤ 5·10^{4}$) — the number of texts of leaflets for expertise.

翻译中…

Each of the next $*m*$ lines contains the only string $*t*_{*i*}$ — the text of the $*i*$-th leaflet. The sum of lengths of all leaflet texts doesn't exceed $5·10^{4}$. The text of the leaflets consists of only lowercase English letters.

翻译中…

The next line contains integer $*q*$ ($1 ≤ *q* ≤ 5·10^{5}$) — the number of queries for expertise.

翻译中…

Finally, each of the last $*q*$ lines contains four integers $*l*$, $*r*$, $*p*_{*l*}$, $*p*_{*r*}$ ($1 ≤ *l* ≤ *r* ≤ *m*, 1 ≤ *p*_{*l*} ≤ *p*_{*r*} ≤ |*s*|$), where $|*s*|$ is the length of the Absolutely Inadmissible Swearword.

翻译中…

Output

翻译中…

Print $*q*$ lines. The $*i*$-th of them should contain two integers — the number of the text with the most occurences and the number of occurences of the substring $[*p*_{*l*}, *p*_{*r*}]$ of the string $*s*$. If there are several text numbers print the smallest one.

翻译中…

样例

Input #1
suffixtree
3
suffixtreesareawesome
cartesiantreeisworsethansegmenttree
nyeeheeheee
2
1 2 1 10
1 3 9 10
Output #1
1 1
3 4
InputCopy OutputCopy
题目信息
题号
666E
时间限制
6 s
内存限制
768 MB
标签
数据结构
后缀结构

题面与样例版权归 Codeforces 及出题人所有;中文翻译由 AI 生成(CI 管线),如有出入请以原文为准。