#P1043G. Speckled Band

    ID: 5091 Type: RemoteJudge 7000ms 512MiB Tried: 0 Accepted: 0 Difficulty: 10 Uploaded By: Tags>data structuresdivide and conquerhashingstring suffix structuresstrings*3500

Speckled Band

Description

Ildar took a band (a thin strip of cloth) and colored it. Formally, the band has nn cells, each of them is colored into one of 2626 colors, so we can denote each color with one of the lowercase letters of English alphabet.

Ildar decided to take some segment of the band [l,r][l, r] (1lrn1 \le l \le r \le n) he likes and cut it from the band. So he will create a new band that can be represented as a string t=slsl+1srt = s_l s_{l+1} \ldots s_r.

After that Ildar will play the following game: he cuts the band tt into some new bands and counts the number of different bands among them. Formally, Ildar chooses 1kt1 \le k \le |t| indexes 1i1<i2<<ik=t1 \le i_1 < i_2 < \ldots < i_k = |t| and cuts tt to kk bands-strings t1t2ti1,ti1+1ti2,,tik1+1tikt_1 t_2 \ldots t_{i_1}, t_{i_1 + 1} \ldots t_{i_2}, \ldots, {t_{i_{k-1} + 1}} \ldots t_{i_k} and counts the number of different bands among them. He wants to know the minimal possible number of different bands he can get under the constraint that at least one band repeats at least two times. The result of the game is this number. If it is impossible to cut tt in such a way, the result of the game is -1.

Unfortunately Ildar hasn't yet decided which segment he likes, but he has qq segments-candidates [l1,r1][l_1, r_1], [l2,r2][l_2, r_2], ..., [lq,rq][l_q, r_q]. Your task is to calculate the result of the game for each of them.

The first line contains one integer nn (1n2000001 \le n \le 200\,000) — the length of the band Ildar has.

The second line contains a string ss consisting of nn lowercase English letters — the band Ildar has.

The third line contains a single integer qq (1q2000001 \le q \le 200\,000) — the number of segments Ildar has chosen as candidates.

Each of the next qq lines contains two integer integers lil_i and rir_i (1lirin1 \le l_i \le r_i \le n) denoting the ends of the ii-th segment.

Output qq lines, where the ii-th of them should contain the result of the game on the segment [li,ri][l_i, r_i].

Input

The first line contains one integer nn (1n2000001 \le n \le 200\,000) — the length of the band Ildar has.

The second line contains a string ss consisting of nn lowercase English letters — the band Ildar has.

The third line contains a single integer qq (1q2000001 \le q \le 200\,000) — the number of segments Ildar has chosen as candidates.

Each of the next qq lines contains two integer integers lil_i and rir_i (1lirin1 \le l_i \le r_i \le n) denoting the ends of the ii-th segment.

Output

Output qq lines, where the ii-th of them should contain the result of the game on the segment [li,ri][l_i, r_i].

Sample Input 1

9
abcabcdce
7
1 6
4 7
5 9
6 9
1 9
3 6
4 4

Sample Output 1

1
-1
4
3
2
2
-1

Note

Consider the first example.

If Ildar chooses the segment [1,6][1, 6], he cuts a string t=abcabct = abcabc. If he cuts tt into two bands abcabc and abcabc, the band abcabc repeats two times and the number of different tapes is 11. So, the result of this game is 11.

If Ildar chooses the segment [4,7][4, 7], he cuts a string t=abcdt = abcd. It is impossible to cut this band in such a way that there is at least one band repeating at least two times. So, the result of this game is 1-1.

If Ildar chooses the segment [3,6][3, 6], he cuts a string t=cabct = cabc. If he cuts tt into three bands cc, abab and cc, the band cc repeats two times and the number of different bands is 22. So, the result of this game is 22.