#Q40. 「一本通 2.1 练习 4」A Horrible Poem

「一本通 2.1 练习 4」A Horrible Poem

Description

Original source: POI 2012

Given a string S S consisting of lowercase English letters, and q q queries, each query asks for the length of the shortest cycle in a substring of S S .

A string B B is a cycle of string A A if A A can be formed by repeating B B multiple times.

Input Format

The first line contains a positive integer n n , the length of S S .
The second line contains n n lowercase English letters, representing the string S S .
The third line contains a positive integer q q , the number of queries.
The following q q lines each contain two positive integers a a and b b , representing the query for the substring S[a..b] S[a..b] and asking for the length of its shortest cycle.

Output Format

Output q q lines of positive integers, where the i i -th line corresponds to the answer for the i i -th query.

Sample 1

8
aaabcabc
3
1 3
3 8
4 8

1
3
5

Constraints and Hints

1abn5×105, 1 \le a \le b \le n \le {5\times 10^5} , q2×106 q \le {2\times 10 ^ 6} .