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;
using ll = long long;
const int N = 200005;
int n, l, r, lg[N], bl, bl2;
struct SA {
  int rk[N], sa[N], h[N], ff[20][N];
  void build(char *s) {
    static int o[N], a[N], f[N], g[N];
    memset(f, 0, 27 * sizeof(int));
    for (int i = 1; i <= n; i++) ++f[s[i] - 'a' + 1];
    for (int i = 1; i < 27; i++) f[i] = !!f[i] + f[i - 1];
    int p = f[26];
    for (int i = 1; i <= n; i++) rk[i] = f[s[i] - 'a' + 1];
    for (int i = 1; p < n; i <<= 1) {
      memcpy(o + 1, rk + 1, sizeof(int) * n);
      memset(f, 0, sizeof(int) * (p + 1));
      for (int j = 1; j <= n; j++) ++f[a[j] = j + i <= n ? rk[j + i] : 0];
      for (int j = 1; j <= p; j++) f[j] += f[j - 1];
      for (int j = 1; j <= n; j++) g[f[a[j]]--] = j;
      memset(f, 0, sizeof(int) * (p + 1));
      for (int j = 1; j <= n; j++) ++f[rk[j]];
      for (int j = 1; j <= p; j++) f[j] += f[j - 1];
      for (int j = n; j > 0; --j) sa[f[rk[g[j]]]--] = g[j];
      p = 0;
      for (int j = 1; j <= n; j++)
        rk[sa[j]] = p += o[sa[j]] > o[sa[j - 1]] ||
                         (o[sa[j]] == o[sa[j - 1]] && a[sa[j]] > a[sa[j - 1]]);
    }
    for (int j = 1; j <= n; j++) sa[rk[j]] = j;
    for (int i = 1, j = 0; i <= n; h[rk[i++]] = j)
      if (rk[i] == n)
        j = 0;
      else
        for (j ? --j : 0; s[i + j] == s[sa[rk[i] + 1] + j]; j++)
          ;
    memcpy(ff[0] + 1, h + 1, sizeof(int) * (n - 1));
    for (int i = 1; (1 << i) < n; i++)
      for (int j = 1; j + (1 << i) - 1 < n; j++)
        ff[i][j] = min(ff[i - 1][j], ff[i - 1][j + (1 << (i - 1))]);
  }
  int lcp(int x, int y) {
    x = rk[x];
    y = rk[y];
    if (x > y) swap(x, y);
    int k = lg[y - x];
    return min(ff[k][x], ff[k][y - (1 << k)]);
  }
  bool check(int l, int r) {
    for (int i = 1; i <= r - l && i <= bl; i++)
      if (lcp(l, r - i + 1) >= i) return 1;
    for (int i = rk[l], lim = min(i + bl2, n), mn = N; i < lim;) {
      mn = min(mn, h[i++]);
      if (l < sa[i] && sa[i] <= r && sa[i] + mn - 1 >= r) return 1;
    }
    for (int i = rk[l], lim = max(i - bl2, 1), mn = N; i > lim;) {
      mn = min(mn, h[--i]);
      if (l < sa[i] && sa[i] <= r && sa[i] + mn - 1 >= r) return 1;
    }
    return 0;
  }
} s0, s1;
unordered_set<ll> st;
vector<pair<int, int>> v[N];
int l1[N], l2[N], r1[N], r2[N];
void ins(int l, int r) {
  int x = s1.lcp(n - r + 1, n - l + 1), y = s0.lcp(l, r), d = r - l;
  l -= x - 1;
  r += y - 1;
  if (r - l + 1 < d * 2 || !st.insert(l * 1ll * N + r).second) return;
  for (int i = l; i + d * 2 - 1 <= r; i++)
    v[i].push_back({r, d}), l1[i] = min(l1[i], i + d * 2 - 1);
  for (int i = r; i - d * 2 + 1 >= l; --i) r1[i] = max(r1[i], i - d * 2 + 1);
}
void work(int tp) {
  static int stk[N];
  int top = 0;
  for (int i = n; i > 0; --i) {
    while (top && (s0.rk[i] < s0.rk[stk[top]]) == tp) --top;
    if (top) ins(i, stk[top]);
    stk[++top] = i;
  }
}
int nxt[N], lst[N], l3[N];
int solve(int l, int r) {
  auto it = lower_bound(v[l].begin(), v[l].end(), pair<int, int>{r, 0});
  if (it != v[l].end() && (r - l + 1) % it->second == 0 &&
      (r - l + 1) >= it->second * 2)
    return 1;
  if (l1[l] < r || r1[r] > l || s0.check(l, r)) return 2;
  return nxt[l] <= r || lst[r] >= l || l2[l] <= r ? 3 : l3[l] <= r ? 4 : -1;
}
char s[N];
int lstc[26];
int main() {
  ios::sync_with_stdio(false);
  for (int i = 2; i < N; i++) lg[i] = lg[i >> 1] + 1;
  cin >> n >> (s + 1);
  bl = sqrt(n);
  bl2 = (n + bl - 1) / bl;
  s0.build(s);
  reverse(s + 1, s + n + 1);
  s1.build(s);
  reverse(s + 1, s + n + 1);
  for (int i = 1; i <= n; i++) l1[i] = N;
  work(0);
  work(1);
  for (int i = 1; i <= n; i++)
    lst[i] = lstc[s[i] - 'a'], lstc[s[i] - 'a'] = i,
    sort(v[i].begin(), v[i].end());
  memset(lstc, 1, 26 * sizeof(int));
  l2[n + 1] = l3[n + 1] = N;
  for (int i = n; i > 0; --i)
    nxt[i] = lstc[s[i] - 'a'], lstc[s[i] - 'a'] = i,
    l2[i] = min(l1[i], l2[i + 1]), l3[i] = min(nxt[i], l3[i + 1]);
  int q;
  cin >> q;
  while (q--) {
    cin >> l >> r;
    cout << solve(l, r) << endl;
  }
  return 0;
}