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;
const int inf = 1e9;
const long long inf64 = 1e18;
struct RangeTree {
  int n = 0;
  vector<int> t;
  RangeTree() = default;
  RangeTree(int nn) : n(nn) { t.resize(4 * n, inf); }
  void upd(int v, int x) { t[v] = min(t[v], x); }
  void push(int v) {
    upd(v << 1, t[v]);
    upd(v << 1 | 1, t[v]);
  }
  void update(int v, int tl, int tr, int l, int r, int x) {
    if (l <= tl && tr <= r) {
      upd(v, x);
    } else {
      int tm = (tl + tr) >> 1;
      push(v);
      if (l <= tm) update(v << 1, tl, tm, l, r, x);
      if (r > tm) update(v << 1 | 1, tm + 1, tr, l, r, x);
    }
  }
  void update(int l, int r, int x) { update(1, 0, n - 1, l, r, x); }
  int get(int v, int tl, int tr, int pos) {
    if (tl == tr)
      return t[v];
    else {
      int tm = (tl + tr) >> 1;
      push(v);
      return pos <= tm ? get(v << 1, tl, tm, pos)
                       : get(v << 1 | 1, tm + 1, tr, pos);
    }
  }
  int get(int pos) { return get(1, 0, n - 1, pos); }
};
struct RMQ {
  vector<int> a;
  int n, LOG;
  vector<int> _log;
  vector<vector<pair<int, int>>> rmq;
  RMQ(const vector<int>& aa) {
    a = aa;
    n = (int)a.size();
    LOG = 0;
    while ((1 << LOG) <= n) LOG++;
    _log.resize(n + 1);
    for (int i = 2; i <= n; i++) {
      _log[i] = _log[i - 1] + 1;
      if ((1 << _log[i]) > i) _log[i]--;
    }
    rmq.resize(LOG, vector<pair<int, int>>(n));
    for (int i = 0; i < n; i++) {
      rmq[0][i] = {a[i], i};
    }
    for (int j = 1; j < LOG; j++) {
      for (int i = 0; i + (1 << j) <= n; i++) {
        rmq[j][i] = min(rmq[j - 1][i], rmq[j - 1][i + (1 << (j - 1))]);
      }
    }
  }
  pair<int, int> get_min(int l, int r) const {
    if (l > r) return make_pair(inf, -1);
    int lg = _log[r - l + 1];
    return min(rmq[lg][l], rmq[lg][r - (1 << lg) + 1]);
  }
};
const int LOG = 21;
struct SuffixArray {
  vector<int> p, c[LOG];
  SuffixArray() = default;
  SuffixArray(string s) {
    s.push_back(0);
    int n = (int)s.size();
    vector<int> pn, cn, cnt;
    p.resize(n);
    for (int i = 0; i < LOG; i++) c[i].resize(n);
    pn.resize(n), cn.resize(n), cnt.assign(300, 0);
    for (int i = 0; i < n; i++) cnt[s[i]]++;
    for (int i = 1; i < (int)cnt.size(); i++) cnt[i] += cnt[i - 1];
    for (int i = n - 1; i >= 0; i--) p[--cnt[s[i]]] = i;
    for (int i = 1; i < n; i++) {
      c[0][p[i]] = c[0][p[i - 1]];
      if (s[p[i]] != s[p[i - 1]]) c[0][p[i]]++;
    }
    for (int lg = 0, k = 1; k < n; k <<= 1, lg++) {
      for (int i = 0; i < n; i++)
        if ((pn[i] = p[i] - k) < 0) pn[i] += n;
      cnt.assign(n, 0);
      for (int i = 0; i < n; i++) cnt[c[lg][pn[i]]]++;
      for (int i = 1; i < (int)cnt.size(); i++) cnt[i] += cnt[i - 1];
      for (int i = n - 1; i >= 0; i--) p[--cnt[c[lg][pn[i]]]] = pn[i];
      for (int l1, r1, l2, r2, i = 1; i < n; i++) {
        cn[p[i]] = cn[p[i - 1]];
        l1 = p[i - 1], r1 = (l1 + k) % n;
        l2 = p[i], r2 = (l2 + k) % n;
        if (c[lg][l1] != c[lg][l2] || c[lg][r1] != c[lg][r2]) cn[p[i]]++;
      }
      c[lg + 1] = cn;
    }
    p.erase(p.begin(), p.begin() + 1);
  }
  int get_lcp(int i, int j) {
    int res = 0;
    for (int lg = LOG - 1; lg >= 0; lg--) {
      if (i + (1 << lg) > (int)p.size() || j + (1 << lg) > (int)p.size())
        continue;
      if (c[lg][i] == c[lg][j])
        i += (1 << lg), j += (1 << lg), res += (1 << lg);
    }
    return res;
  }
};
struct Tandem {
  int l, r, k;
};
vector<int> z_func(const string& s) {
  int n = (int)s.size();
  vector<int> z(n);
  for (int l = 0, r = -1, i = 1; i < n; i++) {
    int k = i > r ? 0 : min(z[i - l], r - i + 1);
    while (i + k < n && s[i + k] == s[k]) k++;
    z[i] = k;
    if (i + k - 1 > r) {
      r = i + k - 1;
      l = i;
    }
  }
  return z;
}
const int SIZE = (2e5 + 5) * 30;
const int MAXL = (2e5 + 5) * 4;
Tandem tandems[SIZE], hlp[MAXL];
int tsz;
void rec(const string& s, int L, int R) {
  if (R - L + 1 <= 1) {
    return;
  }
  int M = (L + R) / 2;
  rec(s, L, M);
  rec(s, M + 1, R);
  int nu = M - L + 1;
  int nv = R - M;
  string vu = s.substr(M + 1, nv) + "#" + s.substr(L, nu);
  string urvr = vu;
  reverse(urvr.begin(), urvr.end());
  vector<int> z1 = z_func(urvr);
  vector<int> z2 = z_func(vu);
  for (int x = L; x <= R; x++) {
    if (x <= M) {
      int k = M + 1 - x;
      int k1 = L < x ? z1[nu - x + L] : 0;
      int k2 = z2[nv + 1 + x - L];
      int lsh = max(0, k - k2);
      int rsh = min(k1, k - 1);
      if (lsh <= rsh) {
        tandems[tsz++] = {x - rsh, x - lsh, k};
      }
    } else {
      int k = x - M;
      int k1 = x < R ? z2[x - M] : 0;
      int k2 = z1[nu + nv - x + M + 1];
      int lsh = max(1, k - k1);
      int rsh = min(k2, k - 1);
      if (lsh <= rsh) {
        tandems[tsz++] = {x - rsh + 1 - k, x - lsh + 1 - k, k};
      }
    }
  }
}
void compress() {
  sort(tandems, tandems + tsz, [](const Tandem& t1, const Tandem& t2) {
    return t1.k < t2.k || (t1.k == t2.k && t1.l < t2.l);
  });
  int hlp_sz = 0;
  for (int i = 0; i < tsz; i++) {
    int j = i;
    while (j + 1 < tsz && tandems[i].k == tandems[j + 1].k &&
           tandems[j].r + 1 == tandems[j + 1].l) {
      j++;
    }
    hlp[hlp_sz++] = {tandems[i].l, tandems[j].r, tandems[j].k};
    i = j;
  }
  memcpy(tandems, hlp, sizeof(Tandem) * hlp_sz);
  tsz = hlp_sz;
}
void main_lorentz(const string& s) {
  int n = (int)s.size();
  tsz = 0;
  rec(s, 0, n - 1);
  compress();
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int n;
  cin >> n;
  string s;
  cin >> s;
  vector<int> poses[26];
  for (int i = 0; i < n; i++) {
    int x = s[i] - 'a';
    poses[x].push_back(i);
  }
  main_lorentz(s);
  SuffixArray sa(s);
  vector<int> p = sa.p, _lcp(n - 1), pos(n);
  for (int i = 0; i < n; i++) pos[p[i]] = i;
  for (int i = 0; i + 1 < n; i++) _lcp[i] = sa.get_lcp(p[i], p[i + 1]);
  RMQ rmq(_lcp);
  int q;
  cin >> q;
  vector<vector<int>> ds(n + 1);
  for (int i = 1; i <= n; i++) {
    for (int j = 2 * i; j <= n; j += i) ds[j].push_back(i);
  }
  RangeTree for_l(n), for_r(n);
  for (int i = 0; i < tsz; i++) {
    for_l.update(tandems[i].l, tandems[i].r, 2 * tandems[i].k);
    for_r.update(tandems[i].l + 2 * tandems[i].k - 1,
                 tandems[i].r + 2 * tandems[i].k - 1, 2 * tandems[i].k);
  }
  int B = 0;
  while (B * B <= n) B++;
  vector<int> best_start(n);
  for (int l = 0; l < n; l++) best_start[l] = l + for_l.get(l) - 1;
  RMQ rmq_start(best_start);
  for (int iter = 0; iter < q; iter++) {
    int l, r;
    cin >> l >> r;
    l--, r--;
    if (r - l + 1 <= 26) {
      vector<int> used(26);
      int ok = 1;
      for (int i = l; i <= r; i++) {
        if (used[s[i] - 'a']) {
          ok = 0;
          break;
        } else
          used[s[i] - 'a'] = 1;
      }
      if (ok) {
        cout << "-1\n";
        continue;
      }
    }
    {
      int ok = 0;
      for (int period : ds[r - l + 1]) {
        int lx = pos[l], rx = pos[l + period];
        if (lx > rx) swap(lx, rx);
        if (rmq.get_min(lx, rx - 1).first >= r - l + 1 - period) {
          ok = 1;
          break;
        }
      }
      if (ok) {
        cout << "1\n";
        continue;
      }
    }
    {
      if (for_l.get(l) <= r - l + 1) {
        cout << "2\n";
        continue;
      }
      if (for_r.get(r) <= r - l + 1) {
        cout << "2\n";
        continue;
      }
      int ok = 0;
      for (int b = 1; b <= B && 2 * b <= r - l + 1; b++) {
        int lx = pos[l];
        int rx = pos[r - b + 1];
        if (lx > rx) swap(lx, rx);
        if (rmq.get_min(lx, rx - 1).first >= b) {
          ok = 1;
          break;
        }
      }
      if (ok) {
        cout << "2\n";
        continue;
      }
      int pl = max(0, pos[l] - B);
      int pr = min(n - 1, pos[l] + B);
      for (int pi = pl; pi <= pr; pi++) {
        int j = p[pi];
        if (j <= l || j > r) continue;
        int b = r - j + 1;
        if (2 * b > r - l + 1) continue;
        int lx = pos[l];
        int rx = pos[r - b + 1];
        if (lx > rx) swap(lx, rx);
        if (rmq.get_min(lx, rx - 1).first >= b) {
          ok = 1;
          break;
        }
      }
      if (ok) {
        cout << "2\n";
        continue;
      }
    }
    {
      auto it =
          upper_bound(poses[s[l] - 'a'].begin(), poses[s[l] - 'a'].end(), l);
      if (it != poses[s[l] - 'a'].end() && *it <= r) {
        cout << "3\n";
        continue;
      }
      it = lower_bound(poses[s[r] - 'a'].begin(), poses[s[r] - 'a'].end(), r);
      if (it != poses[s[r] - 'a'].begin()) {
        it--;
        if (*it >= l) {
          cout << "3\n";
          continue;
        }
      }
      if (rmq_start.get_min(l, r).first <= r) {
        cout << "3\n";
        continue;
      }
    }
    cout << "4\n";
  }
  return 0;
}