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;
void read(int &x) {
  char ch = getchar();
  x = 0;
  while (ch < '0' || ch > '9') ch = getchar();
  while (ch >= '0' && ch <= '9')
    x = (x << 1) + (x << 3) + ch - 48, ch = getchar();
}
char ch[200000 + 1];
int n;
struct HASH {
  int h[200000 + 1], mi[200000 + 1], inv[200000 + 1], p, mo;
  int pw(int x, int k) {
    int res = 1;
    while (k) {
      if (k & 1) res = 1ll * res * x % mo;
      x = 1ll * x * x % mo;
      k >>= 1;
    }
    return res;
  }
  void build(int x, int y) {
    mo = x, p = y;
    mi[0] = 1;
    for (int i = 1; i <= n; i++) mi[i] = 1ll * mi[i - 1] * p % mo;
    inv[n] = pw(mi[n], mo - 2);
    for (int i = n; i >= 1; i--) inv[i - 1] = 1ll * inv[i] * p % mo;
    for (int i = 1; i <= n; i++)
      h[i] = (h[i - 1] + 1ll * (ch[i] - 'a' + 1) * mi[i] % mo) % mo;
  }
  int hash(int l, int r) {
    return 1ll * (h[r] - h[l - 1] + mo) % mo * inv[l] % mo;
  }
} h1, h2;
bool check(int l, int r, int x, int y) {
  return h1.hash(l, r) == h1.hash(x, y) && h2.hash(l, r) == h2.hash(x, y);
}
int Log[200000 + 1];
struct SA {
  int sa[200000 + 1], rk[200000 + 1], ht[200000 + 1], id[200000 + 1],
      oldrk[200000 << 1], buc[200000 + 1], px[200000 + 1],
      ft[200000 + 1][18 + 1];
  int sk;
  void mysort() {
    fill(buc, buc + 1 + sk, 0);
    for (int i = 1; i <= n; i++) ++buc[px[i] = rk[id[i]]];
    for (int i = 1; i <= sk; i++) buc[i] += buc[i - 1];
    for (int i = n; i >= 1; i--) sa[buc[px[i]]--] = id[i];
  }
  bool pd(int x, int y, int z) {
    return oldrk[x] == oldrk[y] && oldrk[x + z] == oldrk[y + z];
  }
  void build(char ch[]) {
    memset(rk, 0, sizeof rk), memset(oldrk, 0, sizeof oldrk);
    sk = 26;
    for (int i = 1; i <= n; i++) rk[id[i] = i] = ch[i] - 'a' + 1;
    mysort();
    for (int w = 1, p = 0; w <= n; w <<= 1, p = 0) {
      for (int i = n - w + 1; i <= n; i++) id[++p] = i;
      for (int i = 1; i <= n; i++)
        if (sa[i] > w) id[++p] = sa[i] - w;
      mysort();
      memcpy(oldrk, rk, sizeof rk);
      sk = 0;
      for (int i = 1; i <= n; i++)
        rk[sa[i]] = pd(sa[i], sa[i - 1], w) ? sk : ++sk;
      if (sk == n) {
        for (int i = 1; i <= n; i++) sa[rk[i]] = i;
        break;
      }
    }
    sk = 0;
    for (int i = 1; i <= n; i++) {
      if (sk) --sk;
      while (ch[i + sk] == ch[sa[rk[i] - 1] + sk]) ++sk;
      ht[rk[i]] = sk;
    }
    for (int i = 1; i <= n; i++) ft[i][0] = ht[i];
    for (int j = 0; j <= 18 - 1; j++)
      for (int i = 1; i <= n; i++)
        ft[i][j + 1] =
            (i + (1 << j) <= n) ? min(ft[i][j], ft[i + (1 << j)][j]) : ft[i][j];
  }
  int dt;
  int get(int l, int r) {
    l = rk[l], r = rk[r];
    if (l > r) swap(l, r);
    if (++l == r) return ft[l][0];
    dt = Log[r - l + 1];
    return min(ft[l][dt], ft[r - (1 << dt) + 1][dt]);
  }
} s1, s2;
struct Tree {
  int vl[200000 << 2];
  Tree() { memset(vl, 0x3f, sizeof vl); }
  void Add(int t, int l, int r, int x, int y, int k, int lz) {
    if (lz < vl[t]) vl[t] = lz;
    if (x <= l && r <= y) {
      vl[t] = k;
      return;
    }
    int mid = l + r >> 1;
    if (x <= mid) Add((t << 1), l, mid, x, y, k, vl[t]);
    if (y > mid) Add((t << 1 | 1), mid + 1, r, x, y, k, vl[t]);
  }
  void fil(int t, int l, int r, int lz, int at[]) {
    if (lz < vl[t]) vl[t] = lz;
    if (l == r) {
      at[l] = vl[t];
      return;
    }
    int mid = l + r >> 1;
    fil((t << 1), l, mid, vl[t], at), fil((t << 1 | 1), mid + 1, r, vl[t], at);
  }
} t1, t2;
int gt[200000 + 1][18 + 1], dl[200000 + 1], dr[200000 + 1], sc[200000 + 1][26];
const int inf = 0x3f3f3f3f;
void Init() {
  Log[1] = 0;
  for (int i = 2; i <= n; i++) Log[i] = Log[i >> 1] + 1;
  h1.build(998244353, 37);
  h2.build(1000000007, 29);
  s1.build(ch);
  for (int i = (n >> 1); i >= 1; i--) swap(ch[i], ch[n - i + 1]);
  s2.build(ch);
  for (int i = (n >> 1); i >= 1; i--) swap(ch[i], ch[n - i + 1]);
  int l, r, lcp, lcs, dlen, pl, pr;
  for (int len = (n >> 1); len >= 1; len--) {
    for (int k = (n / len) - 1; k >= 1; k--) {
      l = len * k, r = len * (k + 1);
      lcs = s1.get(l, r);
      lcp = s2.get(n - l + 1, n - r + 1);
      if (lcp + lcs > len) {
        lcp = min(lcp, len), lcs = min(lcs, len);
        dlen = lcp + lcs - len - 1;
        t1.Add(1, 1, n, l - lcp + 1, l - lcp + 1 + dlen, (len << 1), inf);
        t2.Add(1, 1, n, r + lcs - 1 - dlen, r + lcs - 1, (len << 1), inf);
      }
    }
  }
  t1.fil(1, 1, n, inf, dl), t2.fil(1, 1, n, inf, dr);
  for (int i = 1; i <= n; i++) {
    for (int j = 0; j <= 25; j++) sc[i][j] = sc[i - 1][j];
    ++sc[i][ch[i] - 'a'];
  }
  for (int i = 1; i <= n; i++) gt[i][0] = i + dl[i] - 1;
  for (int j = 0; j <= 18 - 1; j++)
    for (int i = 1; i <= n; i++)
      gt[i][j + 1] =
          (i + (1 << j) <= n) ? min(gt[i][j], gt[i + (1 << j)][j]) : gt[i][j];
}
bool pd0(int l, int r) {
  for (int i = 0; i <= 25; i++)
    if (sc[r][i] - sc[l - 1][i] > 1) return 0;
  return 1;
}
bool pd1(int l, int r) {
  int len = r - l + 1, sq = sqrt(len);
  if (check(l, r - 1, l + 1, r)) return 1;
  for (int i = 2; i <= sq; i++)
    if (len % i == 0)
      if (check(l, r - i, l + i, r) || check(l, r - len / i, l + len / i, r))
        return 1;
  return 0;
}
bool pd2(int l, int r) {
  int len = r - l + 1;
  if (dl[l] <= len || dr[r] <= len) return 1;
  int sq = sqrt(n);
  for (int i = min(sq, (len >> 1)); i >= 1; i--)
    if (check(l, l + i - 1, r - i + 1, r)) return 1;
  if (sq >= (len >> 1)) return 0;
  int min_ht = s1.ht[s1.rk[l]];
  for (int i = s1.rk[l] - 1; i >= s1.rk[l] - sq + 1; i--) {
    if (i < 1) break;
    if ((l + r >> 1) < s1.sa[i] && s1.sa[i] <= r && s1.sa[i] + min_ht > r)
      return 1;
    min_ht = min(min_ht, s1.ht[i]);
  }
  min_ht = len;
  for (int i = s1.rk[l] + 1; i <= s1.rk[l] + sq - 1; i++) {
    if (i > n) break;
    min_ht = min(min_ht, s1.ht[i]);
    if ((l + r >> 1) < s1.sa[i] && s1.sa[i] <= r && s1.sa[i] + min_ht > r)
      return 1;
  }
  return 0;
}
bool pd3(int l, int r) {
  return (sc[r][ch[l] - 'a'] - sc[l][ch[l] - 'a'] > 0) ||
         (sc[r - 1][ch[r] - 'a'] - sc[l - 1][ch[r] - 'a'] > 0);
}
int dx;
int get_Min_dl(int l, int r) {
  if (l == r) return gt[l][0];
  dx = Log[r - l + 1];
  return min(gt[l][dx], gt[r - (1 << dx) + 1][dx]);
}
int main() {
  scanf("%d\n%s", &n, ch + 1);
  Init();
  int T, l, r;
  read(T);
  while (T--) {
    read(l), read(r);
    if (pd0(l, r))
      puts("-1");
    else if (pd1(l, r))
      puts("1");
    else if (pd2(l, r))
      puts("2");
    else if (pd3(l, r) || get_Min_dl(l, r) <= r)
      puts("3");
    else
      puts("4");
  }
  return 0;
}