Problem: Ildar took a band (a thin strip of cloth) and colored it. Formally, the band has n cells, each of them is colored into one of 26 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] (1 ≤ l ≤ r ≤ n) he likes and cut it from the band. So he will create a new band that can be represented as a string t = s_l s_{l+1} … s_r.

After that Ildar will play the following game: he cuts the band t into some new bands and counts the number of different bands among them. Formally, Ildar chooses 1 ≤ k ≤ |t| indexes 1 ≤ i_1 < i_2 < … < i_k = |t| and cuts t to k bands-strings t_1 t_2 … t_{i_1}, t_{i_1 + 1} … t_{i_2}, …, {t_{i_{k-1} + 1}} … 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 t in such a way, the result of the game is -1.

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

Input

The first line contains one integer n (1 ≤ n ≤ 200 000) — the length of the band Ildar has.

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

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

Each of the next q lines contains two integer integers l_i and r_i (1 ≤ l_i ≤ r_i ≤ n) denoting the ends of the i-th segment.

Output

Output q lines, where the i-th of them should contain the result of the game on the segment [l_i, r_i].

Example

Input

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


Output

1
-1
4
3
2
2
-1

Note

Consider the first example.

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

If Ildar chooses the segment [4, 7], he cuts a string t = 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.

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

Solution (in CPP):

Solution:
#include <bits/stdc++.h>
using namespace std;
int a14;
inline int rd(int l, int r) { return rand() % (r - l + 1) + l; }
const int mxn = 2e5 + 3, T = 400;
int n, qn, lg[mxn];
char s[mxn];
int rn[mxn], ln[mxn], sm[26][mxn];
struct SSS {
  int cnt[mxn], sa[mxn], rk[mxn], h[mxn], tmp[mxn * 2];
  char ss[mxn];
  int b[22][mxn];
  bool rv;
  inline int lcp(int l, int r) {
    if (rv) l = n - l + 1, r = n - r + 1;
    l = rk[l], r = rk[r];
    if (l > r) swap(l, r);
    ++l;
    int k = lg[r - l + 1];
    return min(b[k][l], b[k][r - (1 << k) + 1]);
  }
  void W() {
    memcpy(ss, s, sizeof(s));
    if (rv) reverse(ss + 1, ss + n + 1);
    for (int i = 1; i <= n; ++i) rk[i] = ss[i], ++cnt[rk[i]];
    for (int i = 1; i <= 200; ++i) cnt[i] += cnt[i - 1];
    for (int i = n; i; --i) sa[cnt[rk[i]]--] = i;
    for (int i = 0; i <= 200; ++i) cnt[i] = 0;
    for (int k = 1, m = 200;; k *= 2) {
      int p = 0;
      for (int i = n - k + 1; i <= n; ++i) tmp[++p] = i;
      for (int i = 1; i <= n; ++i)
        if (sa[i] > k) tmp[++p] = sa[i] - k;
      for (int i = 1; i <= n; ++i) ++cnt[rk[i]];
      for (int i = 1; i <= m; ++i) cnt[i] += cnt[i - 1];
      for (int i = n; i; --i) sa[cnt[rk[tmp[i]]]--] = tmp[i];
      for (int i = 1; i <= m; ++i) cnt[i] = 0;
      memcpy(tmp + 1, rk + 1, sizeof(int) * n);
      p = 1, rk[sa[1]] = 1;
      for (int i = 2; i <= n; ++i)
        rk[sa[i]] =
            tmp[sa[i]] == tmp[sa[i - 1]] && tmp[sa[i] + k] == tmp[sa[i - 1] + k]
                ? p
                : ++p;
      if (p == n) break;
      m = p;
    }
    for (int i = 1, j, k = 0; i <= n; ++i) {
      if (rk[i] == 1) continue;
      if (k) --k;
      j = sa[rk[i] - 1];
      while (ss[i + k] == ss[j + k]) ++k;
      h[rk[i]] = k;
    }
    for (int i = 1; i <= n; ++i) b[0][i] = h[i];
    for (int j = 1; j <= lg[n]; ++j)
      for (int i = 1; i + (1 << j) - 1 <= n; ++i)
        b[j][i] = min(b[j - 1][i], b[j - 1][i + (1 << (j - 1))]);
  }
} S1, S2;
struct asfdas {
  int mn[mxn * 4], tg[mxn * 4];
  inline void _() { memset(mn, 0x3f, sizeof(mn)); }
  inline void put(int x, int k) { mn[x] = tg[x] = k; }
  inline void down(int x) {
    if (tg[x]) put(x * 2, tg[x]), put(x * 2 + 1, tg[x]), tg[x] = 0;
  }
  inline void up(int x) { mn[x] = min(mn[x * 2], mn[x * 2 + 1]); }
  inline void cg(int lc, int rc, int k, int x = 1, int l = 1, int r = n) {
    if (lc > rc) return;
    if (lc <= l && r <= rc) return put(x, k);
    down(x);
    if (lc <= ((l + r) >> 1)) cg(lc, rc, k, x * 2, l, ((l + r) >> 1));
    if (rc > ((l + r) >> 1)) cg(lc, rc, k, x * 2 + 1, ((l + r) >> 1) + 1, r);
    up(x);
  }
  inline int ask(int lc, int rc, int x = 1, int l = 1, int r = n) {
    if (lc > rc) return 1e9;
    if (lc <= l && r <= rc) return mn[x];
    down(x);
    int res = 1e9;
    if (lc <= ((l + r) >> 1)) res = ask(lc, rc, x * 2, l, ((l + r) >> 1));
    if (rc > ((l + r) >> 1))
      res = min(res, ask(lc, rc, x * 2 + 1, ((l + r) >> 1) + 1, r));
    return res;
  }
  inline void dfs1(int a[], int x = 1, int l = 1, int r = n) {
    if (l == r) return a[l] = mn[x] = l + mn[x] * 2 - 1, void();
    down(x);
    dfs1(a, x * 2, l, ((l + r) >> 1)),
        dfs1(a, x * 2 + 1, ((l + r) >> 1) + 1, r);
    up(x);
  }
  inline void dfs2(int a[], int x = 1, int l = 1, int r = n) {
    if (l == r) return a[l] = l - 2 * mn[x] + 1, void();
    down(x);
    dfs2(a, x * 2, l, ((l + r) >> 1)),
        dfs2(a, x * 2 + 1, ((l + r) >> 1) + 1, r);
  }
} S3, S4;
inline int gg(int l, int r) {
  bool ok = 1;
  for (int c = 0; c < 26; ++c) ok &= sm[c][r] - sm[c][l - 1] <= 1;
  if (ok) return -1;
  int c = s[l] - 'a';
  if (sm[c][r] - sm[c][l - 1] == r - l + 1) return 1;
  int L = r - l + 1;
  for (int i = 2; i * i <= L; ++i)
    if (L % i == 0) {
      if (S1.lcp(l, l + i) >= L - i || S1.lcp(l, l + L / i) >= L - L / i)
        return 1;
    }
  if (ln[l] <= r || rn[r] >= l) return 2;
  int J = (r - l + 1) >> 1;
  for (int i = 1; i <= J && i <= T; ++i)
    if (S1.lcp(l, r - i + 1) >= i) return 2;
  for (int i = S1.rk[l] - 1, k = S1.h[i + 1]; i && i >= S1.rk[l] - 500 && k > T;
       k = min(k, S1.h[i]), --i)
    if (S1.sa[i] >= l && S1.sa[i] <= r && k >= r - S1.sa[i] + 1 &&
        r - S1.sa[i] + 1 <= J)
      return 2;
  for (int i = S1.rk[l] + 1, k = S1.h[i];
       i <= n && i <= S1.rk[l] + 500 && k > T; ++i, k = min(k, S1.h[i]))
    if (S1.sa[i] >= l && S1.sa[i] <= r && k >= r - S1.sa[i] + 1 &&
        r - S1.sa[i] + 1 <= J)
      return 2;
  if (sm[c][r] - sm[c][l]) return 3;
  c = s[r] - 'a';
  if (sm[c][r - 1] - sm[c][l - 1]) return 3;
  if (S3.ask(l, r) <= r) return 3;
  return 4;
}
int main() {
  for (int i = 2; i < mxn; ++i) lg[i] = lg[i >> 1] + 1;
  a14 = scanf("%d%s%d", &n, s + 1, &qn);
  for (int c = 0; c < 26; ++c)
    for (int i = 1; i <= n; ++i) sm[c][i] = sm[c][i - 1] + (s[i] == c + 'a');
  S1.rv = 0, S2.rv = 1, S1.W(), S2.W();
  S3._(), S4._();
  for (int i = n; i; --i)
    for (int j = 2 * i; j <= n; j += i)
      if (s[j] == s[j - i]) {
        int l2 = S1.lcp(j - i, j), l1 = S2.lcp(j - i, j);
        S3.cg(j - i - l1 + 1, j - i + l2 - i, i),
            S4.cg(j - l1 + i, j + l2 - 1, i);
      }
  S3.dfs1(ln), S4.dfs2(rn);
  while (qn--) {
    int l, r;
    a14 = scanf("%d%d", &l, &r);
    printf("%d\n", gg(l, r));
  }
  return 0;
}