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 n, m, p, l, r, cnt[600000], lg[600000], sum[600000], pre[600000][30],
    f[600000], g[600000];
bool cov[600000 << 6];
char s[600000];
struct Hash {
  const long long B1 = 233, mod1 = 998244353, B2 = 377, mod2 = 1000000007;
  long long h1[600000], pw1[600000], h2[600000], pw2[600000];
  Hash() {
    memset(h1, 0, sizeof h1);
    memset(h2, 0, sizeof h2);
  }
  void build() {
    pw1[0] = pw2[0] = 1;
    h1[0] = h2[0] = 0;
    for (int i = 1; i <= n; ++i) {
      pw1[i] = pw1[i - 1] * B1 % mod1;
      h1[i] = (h1[i - 1] * B1 + s[i]) % mod1;
      pw2[i] = pw2[i - 1] * B2 % mod2;
      h2[i] = (h2[i - 1] * B2 + s[i]) % mod2;
    }
  }
  long long qry(int l, int r) {
    long long t1 = (h1[r] - h1[l - 1] * pw1[r - l + 1] % mod1 + mod1) % mod1;
    long long t2 = (h2[r] - h2[l - 1] * pw2[r - l + 1] % mod2 + mod2) % mod2;
    return t1 << 31 | t2;
  }
} H;
struct SA {
  int sa[600000], sa2[600000], x[600000], y[600000], rk[600000],
      lcp[22][600000];
  void resort(int m) {
    memset(cnt, 0, sizeof cnt);
    for (int i = 1; i <= n; ++i) ++cnt[y[i]];
    for (int i = 1; i <= m; ++i) cnt[i] += cnt[i - 1];
    for (int i = 1; i <= n; ++i) sa2[cnt[y[i]]--] = i;
    memset(cnt, 0, sizeof cnt);
    for (int i = 1; i <= n; ++i) ++cnt[x[i]];
    for (int i = 1; i <= m; ++i) cnt[i] += cnt[i - 1];
    for (int i = n; i; --i) sa[cnt[x[sa2[i]]]--] = sa2[i];
    rk[sa[1]] = 1;
    for (int i = 2; i <= n; ++i) {
      int u = sa[i], v = sa[i - 1];
      rk[u] = rk[v] + (!(x[u] == x[v] && y[u] == y[v]));
    }
  }
  void build() {
    for (int i = 1; i <= n; ++i) {
      x[i] = s[i] - 'a' + 1;
      y[i] = 1;
    }
    for (int i = n + 1; i <= n * 2; ++i) rk[i] = 0;
    resort(26);
    for (int k = 1; k < n; k <<= 1) {
      for (int i = 1; i <= n; ++i) {
        x[i] = rk[i];
        y[i] = rk[i + k];
      }
      resort(n);
    }
    for (int i = 1, j, h = 0; i <= n; lcp[0][rk[i++]] = h)
      for (h ? --h : h, j = sa[rk[i] - 1]; j && s[i + h] == s[j + h]; ++h)
        ;
    for (int i = 1; i <= 20; ++i)
      for (int j = 1; j + (1 << i) - 1 <= n; ++j)
        lcp[i][j] = min(lcp[i - 1][j], lcp[i - 1][j + (1 << (i - 1))]);
  }
  int qry(int x, int y) {
    x = rk[x];
    y = rk[y];
    if (x > y) swap(x, y);
    ++x;
    int l = lg[y - x + 1];
    return min(lcp[l][x], lcp[l][y - (1 << l) + 1]);
  }
} sa0, sa1;
namespace p0 {
bool ask(int l, int r) {
  for (int i = 0; i < 26; ++i)
    if (pre[r][i] - pre[l - 1][i] > 1) return 0;
  return 1;
}
}  // namespace p0
namespace p1 {
bool check(int l, int r, int i) { return H.qry(l, r - i) == H.qry(l + i, r); }
bool ask(int l, int r) {
  int len = r - l + 1;
  for (int i = 1; i * i <= len; ++i)
    if (len % i == 0) {
      if (i < len && check(l, r, i)) return 1;
      if (len / i < len && check(l, r, len / i)) return 1;
    }
  return 0;
}
}  // namespace p1
namespace p2 {
bool check_border(int l, int r) {
  int len = r - l + 1;
  for (int i = 1; i <= p && i < len; ++i)
    if (H.qry(l, l + i - 1) == H.qry(r - i + 1, r)) return 1;
  for (int i = 1, mn = n - l + 1; i <= p && sa0.rk[l] + i <= n; ++i) {
    int t = sa0.rk[l] + i, j = sa0.sa[t];
    mn = min(mn, sa0.lcp[0][t]);
    if (j > l && j <= r && j + mn > r) return 1;
  }
  for (int i = 1, mn = n - l + 1; i <= p && sa0.rk[l] - i; ++i) {
    int t = sa0.rk[l] - i, j = sa0.sa[t];
    mn = min(mn, sa0.lcp[0][t + 1]);
    if (j > l && j <= r && j + mn > r) return 1;
  }
  return 0;
}
bool ask(int l, int r) {
  if (f[l] <= r || g[r] >= l) return 1;
  return check_border(l, r);
}
}  // namespace p2
namespace p3 {
int seg[600000 << 1];
void build(int x, int l, int r) {
  if (l == r) {
    seg[x] = f[l];
    return;
  }
  int mid = (l + r) >> 1;
  build((x << 1), l, mid);
  build((x << 1 | 1), mid + 1, r);
  seg[x] = min(seg[(x << 1)], seg[(x << 1 | 1)]);
}
int qmin(int x, int l, int r, int L, int R) {
  if (l >= L && r <= R) return seg[x];
  int mid = (l + r) >> 1, ret = n + 1;
  if (L <= mid) ret = min(ret, qmin((x << 1), l, mid, L, R));
  if (R > mid) ret = min(ret, qmin((x << 1 | 1), mid + 1, r, L, R));
  return ret;
}
bool ask(int l, int r) {
  if (pre[r][s[l] - 'a'] - pre[l][s[l] - 'a']) return 1;
  if (pre[r - 1][s[r] - 'a'] - pre[l - 1][s[r] - 'a']) return 1;
  if (qmin(1, 1, n, l, r) <= r) return 1;
  return 0;
}
}  // namespace p3
void getsqr() {
  for (int i = 1; i <= n; ++i) {
    f[i] = n + 1;
    g[i] = 0;
  }
  for (int i = 1, j = 0; i <= n; ++i) lg[i] = j += (1 << (j + 1)) <= i;
  for (int i = 1; i <= n; ++i) sum[i] = sum[i - 1] + n / i;
  for (int i = 1; i <= n; ++i) {
    int l = 0, r = 0;
    for (int j = i; j + i <= n; j += i)
      if (j > r) {
        l = sa1.qry(n - j + 1, n - (j + i) + 1), r = sa0.qry(j, j + i);
        l = j - l + 1;
        r = j + r - 1;
        if (r - l + 1 < i || cov[sum[i - 1] + j / i]) continue;
        for (int k = i * 2; l + k * 2 - 1 <= r + i; k += i)
          cov[sum[k - 1] + (l + k - 1) / k] = 1;
        for (int k = l; k <= r - i + 1; ++k) {
          f[k] = min(f[k], k + i * 2 - 1);
          g[k + i * 2 - 1] = max(g[k + i * 2 - 1], k);
        }
      }
  }
  p3::build(1, 1, n);
}
int main() {
  scanf("%d%s%d", &n, s + 1, &m);
  p = sqrt(n) + 10;
  H.build();
  sa0.build();
  reverse(s + 1, s + n + 1);
  sa1.build();
  reverse(s + 1, s + n + 1);
  for (int i = 1; i <= n; ++i)
    for (int j = 0; j < 26; ++j) pre[i][j] = pre[i - 1][j] + (s[i] - 'a' == j);
  getsqr();
  while (m--) {
    scanf("%d%d", &l, &r);
    if (p0::ask(l, r))
      puts("-1");
    else if (p1::ask(l, r))
      puts("1");
    else if (p2::ask(l, r))
      puts("2");
    else if (p3::ask(l, r))
      puts("3");
    else
      puts("4");
  }
  return 0;
}