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>
int read() {
  int r = 0, t = 1, c = getchar();
  while (c < '0' || c > '9') {
    t = c == '-' ? -1 : 1;
    c = getchar();
  }
  while (c >= '0' && c <= '9') {
    r = (r << 3) + (r << 1) + (c ^ 48);
    c = getchar();
  }
  return r * t;
}
const int N = 400010;
int n, t[N][26], l[N], cnt, lst, f[N];
int L[N];
char s[N];
int c[N][26];
int d[N], _d[N];
std::vector<int> e[N];
int R[N];
namespace seg {
const int M = 10000000;
int c;
int t[M][2];
int clone(int k) {
  c++;
  t[c][0] = t[k][0];
  t[c][1] = t[k][1];
  return c;
}
int modify(int k, int l, int r, int p) {
  if (l == r) return k ? k : ++c;
  k = clone(k);
  int mid = (l + r) >> 1;
  if (p <= mid)
    t[k][0] = modify(t[k][0], l, mid, p);
  else
    t[k][1] = modify(t[k][1], mid + 1, r, p);
  return k;
}
int query(int k, int l, int r, int p) {
  if (!k || r < p) return r + 1;
  if (l == r) return l;
  int mid = (l + r) >> 1, ans = query(t[k][0], l, mid, p);
  if (ans == mid + 1) ans = query(t[k][1], mid + 1, r, p);
  return ans;
}
int Query(int k, int l, int r, int p) {
  if (!k || l > p) return l - 1;
  if (l == r) return l;
  int mid = (l + r) >> 1, ans = Query(t[k][1], mid + 1, r, p);
  if (ans == mid) ans = Query(t[k][0], l, mid, p);
  return ans;
}
int merge(int u, int v) {
  if (!u || !v) return u + v;
  int k = ++c;
  t[k][0] = merge(t[u][0], t[v][0]);
  t[k][1] = merge(t[u][1], t[v][1]);
  return k;
}
}  // namespace seg
void dfs(int u) {
  for (auto v : e[u]) {
    dfs(v);
    R[u] = seg::merge(R[u], R[v]);
  }
}
int _f[N];
int q[N];
int F[N][20];
int Q(int l, int r) {
  int q = ::q[r];
  for (int i = (19), end_i = (0); i >= end_i; i--)
    if (::l[F[q][i]] >= r - l + 1) q = F[q][i];
  return q;
}
int query(int p, int l, int r, int op) {
  int q = Q(l, r);
  return op == 0 ? seg::Query(R[q], 1, n, p)
                 : seg::query(R[q], 1, n, p + (r - l));
}
int check(int l, int r) {
  int p = l;
  int cnt = 0;
  while (1) {
    int q = query(r, l, p, 0) - (p - l);
    if (p - l == r - q) return p != r;
    p = query(l, q, r, 1);
    if (p - l == r - q) return p != r;
  }
}
std::vector<int> D[N];
int main() {
  n = read();
  scanf("%s", s + 1);
  cnt = lst = 1;
  for (int i = (1), end_i = (n); i <= end_i; i++) {
    int np = ++cnt, nq, p = lst, q;
    lst = cnt;
    int c = s[i] - 'a';
    l[np] = i;
    R[np] = seg::modify(0, 1, n, i);
    while (p && !t[p][c]) {
      t[p][c] = np;
      p = f[p];
    }
    if (!p) {
      f[np] = 1;
      continue;
    }
    q = t[p][c];
    if (l[q] == l[p] + 1) {
      f[np] = q;
      continue;
    }
    nq = ++cnt;
    memcpy(t[nq], t[q], sizeof(t[nq]));
    f[nq] = f[q];
    f[q] = f[np] = nq;
    l[nq] = l[p] + 1;
    while (p && t[p][c] == q) {
      t[p][c] = nq;
      p = f[p];
    }
  }
  for (int i = (1), end_i = (n); i <= end_i; i++) {
    c[i][s[i] - 'a']++;
    for (int j = (0), end_j = (25); j <= end_j; j++) c[i][j] += c[i - 1][j];
  }
  for (int i = (2), end_i = (cnt); i <= end_i; i++) e[f[i]].push_back(i);
  dfs(1);
  for (int i = (2), end_i = (n); i <= end_i; i++)
    if (D[i].empty())
      for (int j = i; j <= n; j += i) D[j].push_back(i);
  q[0] = 1;
  for (int i = (1), end_i = (n); i <= end_i; i++)
    q[i] = t[q[i - 1]][s[i] - 'a'];
  for (int i = (1), end_i = (cnt); i <= end_i; i++) F[i][0] = f[i];
  for (int i = (1), end_i = (19); i <= end_i; i++)
    for (int j = (1), end_j = (cnt); j <= end_j; j++)
      F[j][i] = F[F[j][i - 1]][i - 1];
  for (int i = (1), end_i = (n); i <= end_i; i++) {
    d[i] = _d[i] = n + 1;
    int p = i;
    while (p >= 1) {
      int q = query(p - 1, p, i, 0);
      if (!q) break;
      if (Q(q + 1, i) == Q(q - (i - (q + 1)), q)) {
        d[i] = i - q;
        break;
      }
      p = q;
    }
    p = i;
    while (p <= n) {
      int q = query(p + 1, i, p, 1);
      if (q > n) break;
      q -= p - i;
      if (Q(i, q - 1) == Q(q, q + (q - 1 - i))) {
        _d[i] = q - i;
        break;
      }
      p = q;
    }
  }
  _f[0] = -1;
  for (int i = (1), end_i = (n); i <= end_i; i++)
    _f[i] = std::max(_f[i - 1], i - d[i] - d[i]);
  for (int T = (1), end_T = (read()); T <= end_T; T++) {
    int l = read(), r = read(), ans = -1;
    for (int i = (0), end_i = (25); i <= end_i; i++)
      if (c[r][i] - c[l - 1][i] >= 2) ans = 4;
    if (c[l - 1][s[r] - 'a'] != c[r - 1][s[r] - 'a']) ans = 3;
    if (c[l][s[l] - 'a'] != c[r][s[l] - 'a']) ans = 3;
    if (_f[r] + 1 >= l) ans = 3;
    if (r - d[r] - d[r] + 1 >= l) ans = 2;
    if (l + _d[l] + _d[l] - 1 <= r) ans = 2;
    if (ans != 2 && check(l, r)) ans = 2;
    for (auto i : D[r - l + 1]) {
      if (Q(l, r - (r - l + 1) / i) == Q(l + (r - l + 1) / i, r)) ans = 1;
    }
    printf("%d\n", ans);
  }
  return 0;
}