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;
char s[200005];
struct seq {
  int u, v, d;
  bool chk(int x) { return (x - u) % d == 0 && x >= u && x <= v; }
  int len() { return (u <= v) ? ((v - u) / d + 1) : 0; }
};
seq operator+(seq a, int x) {
  a.u += x;
  a.v += x;
  return a;
}
seq operator-(seq a, int x) {
  a.u -= x;
  a.v -= x;
  return a;
}
seq operator-(int x, seq a) { return seq{x - a.v, x - a.u, a.d}; }
seq operator&(seq a, seq b) {
  if (a.d == b.d) {
    seq w = a;
    w.u = max(w.u, b.u);
    w.v = min(w.v, b.v);
    return w;
  }
  if (b.len() <= 3) swap(a, b);
  if (a.len() <= 3) {
    vector<int> s;
    for (int x = a.u; x <= a.v; x += a.d)
      if (b.chk(x)) s.push_back(x);
    if (s.size() == 0) return seq{2, 1, 1};
    if (s.size() == 1) return seq{s[0], s[0], 1};
    return seq{s[0], s.back(), (s.back() - s[0]) / ((int)s.size() - 1)};
  }
  throw "bu hui";
}
int od[19][200005];
set<int> ap[19][200005];
pair<pair<int, int>, int> t[200005];
int ys[200005];
seq first(pair<int, int> a, pair<int, int> b, int k) {
  if (b.second - b.first + 1 != (1 << k)) throw 0.233;
  int L = a.first, R = a.second - (b.second - b.first);
  set<int>& s = ap[k][od[k][b.first]];
  vector<int> v;
  set<int>::iterator it = s.lower_bound(L);
  while (v.size() < 2 && it != s.end() && *it <= R) v.push_back(*it), ++it;
  it = s.upper_bound(R);
  if (it != s.begin()) {
    int x = *(--it);
    if (x >= L && x != v.back()) v.push_back(x);
  }
  if (v.size() == 0) return seq{2, 1, 1};
  if (v.size() == 1) return seq{v[0], v[0], 1};
  if (v.size() == 2) return seq{v[0], v[1], v[1] - v[0]};
  return seq{v[0], v[2], v[1] - v[0]};
}
seq lps(pair<int, int> a, pair<int, int> b, int k) {
  seq A = first(b, pair<int, int>(a.first, a.first + (1 << k) - 1), k),
      B = first(a, pair<int, int>(b.second - (1 << k) + 1, b.second), k);
  A = (b.second + 1) - A;
  B = B + ((1 << k) - a.first);
  return A & B;
}
vector<seq> qry(int l, int r) {
  int k = 0;
  while ((1 << (k + 1)) <= r - l + 1) ++k;
  vector<seq> t;
  t.push_back(lps(pair<int, int>(l, r), pair<int, int>(l, r), k));
  for (int s = k; s; --s)
    t.push_back(lps(pair<int, int>(l, l + (1 << s) - 1),
                    pair<int, int>(r - (1 << s) + 1, r), s - 1));
  return t;
}
int l2[200005];
struct SA {
  int n, k, sa[200005], t[200005], rnk[200005], qzh[200005], tmpsa[200005],
      tmpr[200005], h[200005], mi[19][200005];
  char s[200005];
  bool same(int a, int b, int p) {
    return t[a] == t[b] && t[a + p] == t[b + p];
  }
  void pre(int m = 500) {
    n = strlen(s);
    s[++n] = 0;
    for (int i = 0; i < n; i++) rnk[i] = s[i], ++qzh[rnk[i]];
    for (int i = 1; i < m; i++) qzh[i] += qzh[i - 1];
    for (int i = n - 1; i >= 0; i--) sa[--qzh[rnk[i]]] = i;
    for (int j = 1; j <= n; j <<= 1) {
      int cur = -1;
      for (int i = n - j; i < n; i++) tmpsa[++cur] = i;
      for (int i = 0; i < n; i++)
        if (sa[i] >= j) tmpsa[++cur] = sa[i] - j;
      for (int i = 0; i < n; i++) tmpr[i] = rnk[tmpsa[i]];
      for (int i = 0; i < m; i++) qzh[i] = 0;
      for (int i = 0; i < n; i++) ++qzh[tmpr[i]];
      for (int i = 1; i < m; i++) qzh[i] += qzh[i - 1];
      for (int i = n - 1; i >= 0; i--)
        t[i] = rnk[i], sa[--qzh[tmpr[i]]] = tmpsa[i];
      m = 0;
      for (int i = 0; i < n; i++)
        rnk[sa[i]] = (i > 0 && same(sa[i], sa[i - 1], j)) ? m : ++m;
      ++m;
    }
    for (int i = 0; i < n; i++) rnk[sa[i]] = i;
    int p = 0;
    for (int i = 0; i < n; i++) {
      if (p) --p;
      int ls = sa[rnk[i] - 1];
      while (s[ls + p] == s[i + p]) p++;
      h[rnk[i]] = p;
    }
    --n;
    for (int i = 1; i <= n; i++) sa[i - 1] = sa[i];
    for (int i = 0; i < n; i++) rnk[sa[i]] = i;
    for (int i = 2; i <= n; i++) h[i - 1] = h[i];
    h[n] = sa[n] = 0;
    for (int i = 0; i < n; i++) mi[0][i] = h[i];
    for (int j = 1; j < 19; j++)
      for (int i = 0; i + (1 << j) <= n; i++)
        mi[j][i] = min(mi[j - 1][i], mi[j - 1][i + (1 << (j - 1))]);
  }
  inline int gmin(int a, int b) {
    int l = l2[b - a + 1];
    return min(mi[l][a], mi[l][b - (1 << l) + 1]);
  }
  int lcp(int a, int b) {
    if (a == b) return n - a;
    if (rnk[a] > rnk[b]) swap(a, b);
    return gmin(rnk[a] + 1, rnk[b]);
  }
} A, B;
const int ps[2] = {999000011, 864888601};
long long po[2][200005], qz[2][200005];
void build_hash(char* s, int N) {
  for (int w = 0; w < 2; ++w) {
    po[w][0] = 1;
    qz[w][0] = 0;
    for (int j = 1; j <= N; ++j) po[w][j] = po[w][j - 1] * 233LL % ps[w];
    for (int j = 1; j <= N; ++j)
      qz[w][j] = (qz[w][j - 1] * 233LL + s[j - 1]) % ps[w];
  }
}
long long ghash(int l, int r) {
  long long a = r - l + 1;
  for (int w = 0; w < 2; ++w) {
    int u = (qz[w][r + 1] - qz[w][l] * (long long)po[w][r - l + 1]) % ps[w];
    if (u < 0) u += ps[w];
    a = a * 1000000007LL + u;
  }
  return a;
}
void pre() {
  for (int i = 1; i <= n; ++i) od[0][i] = s[i];
  for (int j = 1; j < 19; ++j) {
    int l = 1 << j, u = n - l + 1;
    if (u <= 0) break;
    for (int i = 1; i <= u; ++i)
      t[i] = make_pair(
          pair<int, int>(od[j - 1][i], od[j - 1][i + (1 << (j - 1))]), i);
    sort(t + 1, t + 1 + u);
    int x = 1;
    for (int i = 1; i <= u; ++i) {
      if (i != 1 && t[i].first != t[i - 1].first) ++x;
      od[j][t[i].second] = x;
    }
  }
  for (int j = 0; j < 19; ++j)
    for (int i = 1; i <= n - (1 << j) + 1; ++i) ap[j][od[j][i]].insert(i);
}
int si[200005], ml[200005], mr[200005];
long long med[200005];
bool cov[6666666];
bool np[200005];
vector<int> yy[200005];
vector<int> app[26];
int main() {
  l2[0] = -1;
  for (int i = 1; i < 200005; ++i) l2[i] = l2[i >> 1] + 1;
  scanf("%d%s", &n, s + 1);
  for (int i = 0; i < 26; ++i) app[i].push_back(0);
  for (int i = 1; i <= n; ++i) app[s[i] - 'a'].push_back(i);
  for (int i = 0; i < 26; ++i) app[i].push_back(n + 1);
  for (int i = 2; i <= n; ++i)
    if (!np[i])
      for (int j = i; j <= n; j += i) yy[j].push_back(i), np[j] = 1;
  for (int i = 1; i <= n; ++i) ml[i] = mr[i] = 2e9;
  for (int i = 1; i <= n; ++i) A.s[i - 1] = B.s[n - i] = s[i];
  A.pre();
  B.pre();
  build_hash(s, n + 1);
  pre();
  for (int i = 1; i <= n; ++i) si[i] = si[i - 1] + n / i - 1;
  for (int len = 1; len + len <= n; ++len) {
    int l = -1, r = -1;
    for (int i = len - 1; i + len < n; i += len) {
      if (i <= r) continue;
      int L = B.lcp(n - i - 1, n - i - len - 1), R = A.lcp(i, i + len);
      if (L + R - 1 < len) continue;
      l = i - L + 1;
      r = i + R - 1;
      if (cov[si[len - 1] + (i + 1) / len]) continue;
      for (int x = len + len; l + x + x - 1 <= r + len; x += len)
        cov[si[x - 1] + l / x + 1] = 1;
      for (int t = l; t + len - 1 <= r; ++t)
        ml[t + 1] = min(ml[t + 1], len),
               mr[t + len * 2] = min(mr[t + len * 2], len);
    }
  }
  med[n + 1] = n + 1;
  for (int i = n; i >= 1; --i) med[i] = min(med[i + 1], i + ml[i] * 2LL - 1);
  int q, l, r;
  scanf("%d", &q);
  while (q--) {
    scanf("%d%d", &l, &r);
    if (r - l + 1 <= 26) {
      int sb = 0, gg = 0;
      for (int j = l; j <= r; ++j)
        if (sb & (1 << (s[j] - 'a')))
          gg = 1;
        else
          sb |= 1 << (s[j] - 'a');
      if (!gg) {
        puts("-1");
        continue;
      }
    }
    for (auto j : yy[r - l + 1]) {
      int p = (r - l + 1) / j * (j - 1);
      if (ghash(l, l + p - 1) == ghash(r - p + 1, r)) {
        puts("1");
        goto good;
      }
    }
    if (min(ml[l], mr[r]) * 2LL <= r - l + 1) {
      puts("2");
      goto good;
    }
    {
      vector<seq> ps = qry(l, r);
      int ans = 2e9;
      for (auto t : ps)
        if (t.u <= t.v) ans = min(ans, t.u);
      if (ans * 2LL <= r - l + 1) {
        puts("2");
        goto good;
      }
    }
    if (med[l] <= r ||
        *lower_bound(app[s[l] - 'a'].begin(), app[s[l] - 'a'].end(), l + 1) <=
            r ||
        *lower_bound(app[s[r] - 'a'].begin(), app[s[r] - 'a'].end(), l) < r) {
      puts("3");
      goto good;
    }
    puts("4");
  good:;
  }
}