ICPC Latin America Subregionals · Problem G

Genes

2026

Statement

A DNA sequence is represented by a string TT. Several genes are also known, represented by strings and ordered from most relevant to least relevant. The genes are numbered sequentially, starting from 1.

QQ queries are made. Each delimits a segment of TT between indices LiL_i and RiR_i, inclusive. For each query, determine the index of the most relevant gene that occurs entirely as a substring of that segment.

Input

The first line contains TT (1≤∣T∣≤1051\le|T|\le10^5). The second line contains the number GG of genes (1≤G≤5⋅1051\le G\le5\cdot10^5). Each of the following GG lines contains one gene, from most to least relevant. Every gene is nonempty. The genes are distinct, and the sum SS of their lengths is at most 5⋅1055\cdot10^5.

The next line contains QQ (1≤Q≤1051\le Q\le10^5). Each of the following QQ lines contains Li,RiL_i,R_i (1≤Li≤Ri≤∣T∣1\le L_i\le R_i\le|T|), the inclusive query bounds.

All strings contain only the letters A, C, G, and T.

Output

For each query, print the index of the most relevant gene that occurs entirely within the corresponding segment. If none occurs, print −1-1.

Examples

Input
ACAGACA
3
ACA
G
CA
3
1 7
2 6
1 2

Output
1
2
-1

In the first query, the segment is the entire sequence ACAGACA; its most relevant gene is ACA (index 1). In the second, the segment is CAGAC; its most relevant gene is G (index 2). In the third, the segment is AC, which contains no gene.

Input
ACGT
2
ACGT
GT
2
1 3
2 4

Output
-1
2

Tutorial

We use Aho–Corasick to enumerate every occurrence of every gene in TT.

Bounding the number of matches

Lemma. The total number of gene occurrences in TT is O(∣T∣S)O(|T|\sqrt S), where SS is the sum of the gene lengths.

Consider all genes whose occurrences end at one fixed position of TT. Since the genes are distinct, two of them cannot have the same length: the substring with a given length and ending position is unique. If there are kk such genes, their lengths are at least 1,2,…,k1,2,\ldots,k. Therefore

1+2+⋯+k≤S.1+2+\cdots+k \le S.

Since this sum is Θ(k2)\Theta(k^2), we have k=O(S)k=O(\sqrt S). Applying this bound at every text position proves the lemma.

Processing queries offline

Group each query [l,r][l,r] by its right endpoint rr, then scan TT from left to right with the Aho–Corasick automaton.

Suppose position rr has just been processed. For every gene occurrence ending there, let pp be its starting position and gg its gene index. At position pp, store the minimum index of any occurrence seen so far that starts there.

All stored occurrences already end at or before rr. Consequently, the answer to query [l,r][l,r] is simply the minimum stored gene index over starting positions p≥lp\ge l. This condition guarantees that the entire occurrence is inside the query interval.

We need a data structure supporting:

  1. update(p, g): replace the value at position pp with its minimum with gg;
  2. suffix_min(l): find the minimum value at positions l,l+1,…,∣T∣l,l+1,\ldots,|T|.

Square-root decomposition

Split the starting positions into blocks of size B≈∣T∣B\approx\sqrt{|T|}. Store both the individual value at every position and the minimum value of each block.

An update only decreases one individual value and its block minimum, so it takes O(1)O(1). For a suffix query, inspect individual positions until reaching a block boundary, use the precomputed minimum for every complete block, and inspect the final partial block. This takes

O(∣T∣+∣T∣∣T∣)=O(∣T∣).O\left(\sqrt{|T|}+\frac{|T|}{\sqrt{|T|}}\right)=O(\sqrt{|T|}).

Aho–Corasick construction costs O(S)O(S). Match enumeration performs O(∣T∣S)O(|T|\sqrt S) constant-time updates, and the QQ queries cost O(Q∣T∣)O(Q\sqrt{|T|}). The final complexity is

O(S+∣T∣S+Q∣T∣)O\left(S+|T|\sqrt S+Q\sqrt{|T|}\right)

time and O(S+∣T∣+Q)O(S+|T|+Q) memory.

Reference implementation