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;
long long gcd(long long a, long long b) { return b == 0 ? a : gcd(b, a % b); }
struct ZAlgo {
  string s;
  int n;
  vector<int> z;
  void init(int _n) {
    n = _n;
    s = string(n, '?');
    z = vector<int>(n, -1);
  }
  void calc() {
    int l = 0, r = 0;
    for (int i = (1); i < (n); ++i) {
      if (i < r) {
        int k = i - l;
        if (z[k] < r - i) {
          z[i] = z[k];
          continue;
        } else
          l = i;
      } else
        l = r = i;
      while (r < n && s[r] == s[r - l]) ++r;
      z[i] = r - l;
    }
  }
};
struct RepeatRun {
  int l, r, len;
  RepeatRun(int l, int r, int len) : l(l), r(r), len(len) {}
};
struct RepeatFinder {
  char *s;
  int n;
  ZAlgo ba, abrev;
  vector<RepeatRun> runs;
  void rec(int l, int r) {
    if (l == r) return;
    int m = l + (r - l) / 2;
    rec(l, m);
    rec(m + 1, r);
    ba.n = 0;
    for (int i = (m + 1); i <= (r); ++i) ba.s[ba.n++] = s[i];
    ba.s[ba.n++] = '#';
    for (int i = (l); i <= (m); ++i) ba.s[ba.n++] = s[i];
    abrev.n = 0;
    for (int i = m; i >= l; --i) abrev.s[abrev.n++] = s[i];
    abrev.s[abrev.n++] = '#';
    for (int i = r; i >= m + 1; --i) abrev.s[abrev.n++] = s[i];
    ba.calc();
    abrev.calc();
    for (int i = (l); i <= (m); ++i) {
      int k = m - i + 1, k1 = abrev.z[k], k2 = ba.z[r - m + 1 + i - l];
      if (k1 + k2 >= k && k2 >= 1)
        runs.push_back(RepeatRun(i - min(k - 1, k1), i - (k - k2), k));
    }
    for (int i = (m + 2); i <= (r); ++i) {
      int k = i - m, k1 = ba.z[k], k2 = abrev.z[m - l + 1 + 1 + r - i];
      if (k1 + k2 >= k && k2 >= 1 && k1 >= 1)
        runs.push_back(
            RepeatRun(m + 1 - min(k - 1, k2), m + 1 - max(k - k1, 1), k));
    }
  }
  void init(char *_s) {
    s = _s;
    n = strlen(s);
    ba.init(n + 1);
    abrev.init(n + 1);
  }
  void calcruns() {
    runs.clear();
    rec(0, n - 1);
  }
  vector<int> calcminstart() {
    vector<vector<int>> e(n + 1);
    for (int i = (0); i < (((int)(runs).size())); ++i)
      e[runs[i].l].push_back(runs[i].len),
          e[runs[i].r + 1].push_back(-runs[i].len);
    multiset<int> have;
    vector<int> ret(n);
    for (int i = (0); i < (n); ++i) {
      for (int j = (0); j < (((int)(e[i]).size())); ++j) {
        int x = e[i][j];
        if (x > 0)
          have.insert(x);
        else
          have.erase(have.find(-x));
      }
      ret[i] = ((int)(have).size()) == 0 ? 0 : *have.begin();
    }
    return ret;
  }
  vector<int> calcminend() {
    vector<vector<int>> e(n + 1);
    for (int i = (0); i < (((int)(runs).size())); ++i)
      e[runs[i].l + 2 * runs[i].len - 1].push_back(runs[i].len),
          e[runs[i].r + 2 * runs[i].len].push_back(-runs[i].len);
    multiset<int> have;
    vector<int> ret(n);
    for (int i = (0); i < (n); ++i) {
      for (int j = (0); j < (((int)(e[i]).size())); ++j) {
        int x = e[i][j];
        if (x > 0)
          have.insert(x);
        else
          have.erase(have.find(-x));
      }
      ret[i] = ((int)(have).size()) == 0 ? 0 : *have.begin();
    }
    return ret;
  }
};
struct RMQ {
  int *a;
  int n;
  void init(int *_a, int _n) {
    a = _a;
    n = _n;
  }
  vector<int> lg;
  vector<vector<int>> rmq;
  void calc() {
    lg = vector<int>(n + 1);
    lg[1] = 0;
    for (int i = (1); i <= (n); ++i) {
      lg[i] = lg[i - 1];
      while ((2 << lg[i]) <= i) ++lg[i];
    }
    rmq = vector<vector<int>>(n, vector<int>(lg[n] + 1));
    for (int i = (0); i < (n); ++i) rmq[i][0] = a[i];
    for (int k = (0); k < (lg[n]); ++k)
      for (int i = (0); i < (n); ++i)
        if (i + (1 << k) < n)
          rmq[i][k + 1] = min(rmq[i][k], rmq[i + (1 << k)][k]);
        else
          rmq[i][k + 1] = rmq[i][k];
  }
  int query(int l, int r) {
    int k = lg[r - l + 1];
    return min(rmq[l][k], rmq[r - (1 << k) + 1][k]);
  }
};
struct SuffixArray {
  char *s;
  int n;
  void init(char *_s, bool cyclic) {
    s = _s;
    n = strlen(s) + (cyclic ? 0 : 1);
  }
  vector<int> sa, id, cnt, sa2, oid;
  int nid;
  void calcsuffixarray() {
    sa = id = sa2 = oid = vector<int>(n);
    nid = 256;
    cnt = vector<int>(max(n, nid), 0);
    for (int i = (0); i < (n); ++i) oid[i] = (int)s[i];
    for (int i = (0); i < (n); ++i) ++cnt[oid[i]];
    for (int i = (1); i < (nid); ++i) cnt[i] += cnt[i - 1];
    for (int i = (0); i < (n); ++i) sa[--cnt[oid[i]]] = i;
    nid = 0;
    for (int i = (0); i < (n); ++i)
      if (i == 0 || oid[sa[i]] != oid[sa[i - 1]])
        id[sa[i]] = nid++;
      else
        id[sa[i]] = id[sa[i - 1]];
    for (int step = 0; (1 << step) < n; ++step) {
      for (int i = (0); i < (n); ++i) {
        oid[i] = id[i], sa2[i] = sa[i] - (1 << step);
        if (sa2[i] < 0) sa2[i] += n;
      }
      for (int i = (0); i < (nid); ++i) cnt[i] = 0;
      for (int i = (0); i < (n); ++i) ++cnt[oid[sa2[i]]];
      for (int i = (1); i < (nid); ++i) cnt[i] += cnt[i - 1];
      for (int i = n - 1; i >= 0; --i) sa[--cnt[oid[sa2[i]]]] = sa2[i];
      nid = 0;
      for (int i = (0); i < (n); ++i)
        if (i == 0 || oid[sa[i]] != oid[sa[i - 1]] ||
            oid[(sa[i] + (1 << step)) % n] !=
                oid[(sa[i - 1] + (1 << step)) % n])
          id[sa[i]] = nid++;
        else
          id[sa[i]] = id[sa[i - 1]];
    }
  }
  vector<int> pos, lcp;
  void calcadjacentlcp() {
    pos = vector<int>(n);
    for (int i = (0); i < (n); ++i) pos[sa[i]] = i;
    lcp = vector<int>(n - 1);
    int k = 0;
    for (int i = (0); i < (n); ++i)
      if (pos[i] != n - 1) {
        while (i + k < n && sa[pos[i] + 1] + k < n &&
               s[i + k] == s[sa[pos[i] + 1] + k])
          ++k;
        lcp[pos[i]] = k;
        k = max(0, k - 1);
      }
  }
  RMQ rmq;
  void preparegenerallcp() {
    rmq.init(lcp.data(), n - 1);
    rmq.calc();
  }
  int querylcp(int l, int r) {
    l = pos[l], r = pos[r];
    if (l > r) swap(l, r);
    if (l == r) return n - l;
    return rmq.query(l, r - 1);
  }
};
struct Seg2D {
  int *a;
  int n, lg;
  vector<vector<int>> vals;
  void init(int dep, int l, int r) {
    if (l == r) {
      vals[dep][l] = a[l];
    } else {
      int m = l + (r - l) / 2;
      init(dep + 1, l, m);
      init(dep + 1, m + 1, r);
      merge(vals[dep + 1].begin() + l, vals[dep + 1].begin() + m + 1,
            vals[dep + 1].begin() + m + 1, vals[dep + 1].begin() + r + 1,
            vals[dep].begin() + l);
    }
  }
  bool find(int dep, int l, int r, int L, int R, int LVAL, int RVAL) {
    if (L <= l && r <= R) {
      int off =
          lower_bound(vals[dep].begin() + l, vals[dep].begin() + r + 1, LVAL) -
          vals[dep].begin();
      return off <= r && vals[dep][off] <= RVAL;
    } else {
      int m = l + (r - l) / 2;
      if (L <= m && find(dep + 1, l, m, L, R, LVAL, RVAL)) return true;
      if (m + 1 <= R && find(dep + 1, m + 1, r, L, R, LVAL, RVAL)) return true;
      return false;
    }
  }
  void init(int *_a, int _n) {
    a = _a, n = _n, lg = 0;
    while ((1 << lg) < n) ++lg;
    vals = vector<vector<int>>(lg + 1, vector<int>(n));
    init(0, 0, n - 1);
  }
};
const int MAXLEN = 200000;
const int MAXQ = 200000;
const int ALPH = 26;
int slen;
char s[MAXLEN + 1];
int nq;
int ql[MAXQ], qr[MAXQ], qans[MAXQ];
int charsum[MAXLEN + 1][ALPH];
RepeatFinder repeatfinder;
int firstrepeatends[MAXLEN];
int lastrepeatstarts[MAXLEN];
SuffixArray suffixarray;
int pmn[MAXLEN + 1];
RMQ nextrepeatends;
void precalc() {
  for (int k = (0); k < (ALPH); ++k) charsum[0][k] = 0;
  for (int i = (0); i < (slen); ++i)
    for (int k = (0); k < (ALPH); ++k)
      charsum[i + 1][k] = charsum[i][k] + (s[i] == 'a' + k ? 1 : 0);
  repeatfinder.init(s);
  repeatfinder.calcruns();
  vector<int> minstart = repeatfinder.calcminstart(),
              minend = repeatfinder.calcminend();
  for (int i = (0); i < (slen); ++i)
    firstrepeatends[i] = minstart[i] == 0 ? slen : i + 2 * minstart[i] - 1;
  for (int i = (0); i < (slen); ++i)
    lastrepeatstarts[i] = minend[i] == 0 ? -1 : i - 2 * minend[i] + 1;
  nextrepeatends.init(firstrepeatends, slen);
  nextrepeatends.calc();
  suffixarray.init(s, false);
  suffixarray.calcsuffixarray();
  suffixarray.calcadjacentlcp();
  suffixarray.preparegenerallcp();
  memset(pmn, -1, sizeof(pmn));
  for (int i = (2); i <= (MAXLEN); ++i)
    if (pmn[i] == -1)
      for (int j = i; j <= MAXLEN; j += i)
        if (pmn[j] == -1) pmn[j] = i;
}
Seg2D seg;
bool qborder[MAXQ];
void precalcborders() {
  vector<vector<int>> e(slen + 1);
  for (int i = (0); i < (slen); ++i) e[suffixarray.lcp[i]].push_back(i);
  vector<int> id(slen + 1);
  vector<set<int>> lst(slen + 1);
  for (int i = (0); i <= (slen); ++i) {
    id[i] = i;
    lst[i].insert(i);
  }
  vector<vector<int>> bylen(slen + 1);
  for (int i = (0); i < (nq); ++i)
    qborder[i] = false, bylen[qr[i] - ql[i] + 1].push_back(i);
  vector<set<pair<int, int>>> q(slen + 1);
  for (int lcplen = slen; lcplen >= 1; --lcplen) {
    for (int i = (0); i < (((int)(e[lcplen]).size())); ++i) {
      int idx = e[lcplen][i], aid = id[suffixarray.sa[idx]],
          bid = id[suffixarray.sa[idx + 1]];
      if (((int)(lst[aid]).size()) < ((int)(lst[bid]).size())) swap(aid, bid);
      for (auto it = lst[bid].begin(); it != lst[bid].end(); ++it) {
        int at = *it;
        while (true) {
          auto qit = q[aid].lower_bound(make_pair(at, 0));
          if (qit == q[aid].end() || at + lcplen - 1 < qit->first) break;
          qborder[qit->second] = true;
          q[aid].erase(qit);
        }
      }
      for (auto qit = q[bid].begin(); qit != q[bid].end(); ++qit) {
        int to = qit->first;
        auto it = lst[aid].lower_bound(to - lcplen + 1);
        if (it != lst[aid].end() && *it <= to)
          qborder[qit->second] = true;
        else
          q[aid].insert(make_pair(to, qit->second));
      }
      for (auto it = lst[bid].begin(); it != lst[bid].end(); ++it) {
        int at = *it;
        id[at] = aid, lst[aid].insert(at);
      }
    }
    for (int i = (0); i < (((int)(bylen[lcplen]).size())); ++i) {
      int qid = bylen[lcplen][i], aid = id[ql[qid]];
      q[aid].insert(make_pair(qr[qid], qid));
    }
  }
  seg.init(suffixarray.sa.data(), slen + 1);
  for (int i = (0); i < (nq); ++i)
    if (!qborder[i]) {
      int qlen = qr[i] - ql[i] + 1, pos = suffixarray.pos[ql[i]], lpos, rpos;
      {
        int lo = -1, hi = pos;
        while (lo + 1 < hi) {
          int mi = lo + (hi - lo) / 2;
          if (suffixarray.querylcp(suffixarray.sa[mi], ql[i]) >= qlen)
            hi = mi;
          else
            lo = mi;
        }
        lpos = hi;
      }
      {
        int lo = pos, hi = slen + 1;
        while (lo + 1 < hi) {
          int mi = lo + (hi - lo) / 2;
          if (suffixarray.querylcp(suffixarray.sa[mi], ql[i]) >= qlen)
            lo = mi;
          else
            hi = mi;
        }
        rpos = lo;
      }
      if (seg.find(0, 0, slen, lpos, rpos, ql[i] + 1, qr[i])) qborder[i] = true;
    }
}
int countchar(int l, int r, char c) {
  return charsum[r + 1][c - 'a'] - charsum[l][c - 'a'];
}
bool impossible(int l, int r) {
  for (char c = 'a'; c <= 'z'; ++c)
    if (countchar(l, r, c) >= 2) return false;
  return true;
}
bool periodic(int l, int r) {
  int len = r - l + 1, x = len;
  while (x != 1) {
    int p = pmn[x], repcnt = p, replen = len / repcnt;
    if (suffixarray.querylcp(l, l + replen) >= len - replen) return true;
    while (x % p == 0) x /= p;
  }
  return false;
}
bool startswithrepeat(int l, int r) { return firstrepeatends[l] <= r; }
bool endswithrepeat(int l, int r) { return lastrepeatstarts[r] >= l; }
bool hasborder(int l, int r, int qid) { return qborder[qid]; }
bool containschar(int l, int r, char c) { return countchar(l, r, c) >= 1; }
bool containsrepeat(int l, int r) { return nextrepeatends.query(l, r) <= r; }
int calc(int l, int r, int qid) {
  if (impossible(l, r)) return -1;
  if (periodic(l, r)) return 1;
  if (startswithrepeat(l, r) || endswithrepeat(l, r) || hasborder(l, r, qid))
    return 2;
  if (containschar(l + 1, r, s[l]) || containschar(l, r - 1, s[r]) ||
      containsrepeat(l, r))
    return 3;
  return 4;
}
void solve() {
  precalc();
  precalcborders();
  for (int i = (0); i < (nq); ++i) qans[i] = calc(ql[i], qr[i], i);
}
void run() {
  scanf("%d", &slen);
  scanf("%s", s);
  scanf("%d", &nq);
  for (int i = (0); i < (nq); ++i)
    scanf("%d%d", &ql[i], &qr[i]), --ql[i], --qr[i];
  solve();
  for (int i = (0); i < (nq); ++i) printf("%d\n", qans[i]);
}
int main() {
  run();
  return 0;
}