QOJ.ac

QOJ

Time Limit: 4 s Memory Limit: 128 MB Total points: 100

#13303. A Horrible Poem

Statistics

Bytie boy has to learn a fragment of a certain poem by heart. The poem, following the best lines of modern art, is a long string consisting of lowercase English alphabet letters only. Obviously, it sounds horrible, but that is the least of Bytie's worries. First and foremost, he completely forgot which fragment is he supposed to learn. And they all look quite difficult to memorize...

There is hope, however: some parts of the poem exhibit certain regularity. In particular, every now and then a fragment, say A, is but a multiple repetition of another fragment, say B (in other words, A=BB…B, i.e., A=B^{k}, where k ≥ 1 is an integer). In such case we say that B is a full period of A (in particular, every string is its own full period). If a given fragment has a short full period, Bytie's task will be easy. The question remains... which fragment was that?

Make a gift for Bytie - write a program that will read the whole poem as well as a list of fragments that Bytie suspects might be the one he is supposed to memorize, and for each of them determines its shortest full period.

Input Format

In the first line of the standard input there is a single integer n (1 ≤ n ≤ 500,000). In the second line there is a string of length n consisting of lowercase English alphabet letters-the poem. We number the positions of its successive characters from 1 to n.

The next line holds a single integer q (1 ≤ q ≤ 2,000,000) denoting the number of fragments. Then the following q lines give queries, one per line. Each query is a pair of integers a_{i }and b_{i} (1 ≤ a_{i} ≤ b_{i} ≤ n), separated by a single space, such that the answer to the query should be the length of the shortest full period of the poem's fragment that begins at position a_{i} and ends at position b_{i}.

In tests worth in total 42% of the points n ≤ 10,000 holds in addition. In some of those, worth 30% of points in total, q ≤ 10,000 holds as well.

Output Format

Your program should print q lines on the standard output. The i-th of these lines should hold a single integer - the answer to the i-th query.

Example

Input

8
aaabcabc
3
1 3
3 8
4 8

Output

1
3
5

Discussions

About Discussions

The discussion section is only for posting: Editorials, General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues. Submitting multiple issues may cause your account to be banned.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.