A Horrible Poem

No attempts yetTime limit8sMemory limit128 MB

Problem

Bytie has to memorize a fragment of a certain poem. The poem, in the spirit of modern art, is a long string made up only of lowercase English letters. It sounds horrible, but that is the least of Bytie's worries: he has completely forgotten which fragment he is supposed to learn, and every fragment looks hard to memorize.

There is hope, however, because some parts of the poem are regular. Every now and then a fragment AA is simply another fragment BB repeated several times, that is, A=BBB=BkA = BB\cdots B = B^k for some integer k1k \ge 1. In that case we say that BB is a full period of AA (in particular, every string is a full period of itself). A fragment with a short full period is easy to memorize.

Given the whole poem and the list of fragments Bytie suspects, determine, for each fragment, the length of its shortest full period.

Input

The first line contains an integer nn (1n500,0001 \le n \le 500{,}000). The second line contains a string of length nn consisting of lowercase English letters, the poem; its characters are numbered from 11 to nn.

The next line contains an integer qq (1q2,000,0001 \le q \le 2{,}000{,}000), the number of fragments. Each of the next qq lines contains two integers aia_i and bib_i (1aibin1 \le a_i \le b_i \le n), separated by a single space, describing the fragment that starts at position aia_i and ends at position bib_i.

Output

Print qq lines. The ii-th line should contain a single integer: the length of the shortest full period of the ii-th fragment.