#include <bits/stdc++.h>
using namespace std;
const int mod = 1e9 + 7;
struct fastio {
  char s[100000];
  int it, len;
  fastio() { it = len = 0; }
  inline char get() {
    if (it < len) return s[it++];
    it = 0;
    len = fread(s, 1, 100000, stdin);
    if (len == 0)
      return EOF;
    else
      return s[it++];
  }
  bool notend() {
    char c = get();
    while (c == ' ' || c == '\n') c = get();
    if (it > 0) it--;
    return c != EOF;
  }
} _buff;
inline long long getnum() {
  long long r = 0;
  bool ng = 0;
  char c;
  c = _buff.get();
  while (c != '-' && (c < '0' || c > '9')) c = _buff.get();
  if (c == '-') ng = 1, c = _buff.get();
  while (c >= '0' && c <= '9') r = r * 10 + c - '0', c = _buff.get();
  return ng ? -r : r;
}
template <class T>
inline void putnum(T x) {
  if (x < 0) putchar('-'), x = -x;
  register short a[20] = {}, sz = 0;
  while (x) a[sz++] = x % 10, x /= 10;
  if (sz == 0) putchar('0');
  for (int i = sz - 1; i >= 0; i--) putchar('0' + a[i]);
}
inline char getreal() {
  char c = _buff.get();
  while (c <= 32) c = _buff.get();
  return c;
}
long long qpow(long long x, long long k) {
  return k == 0 ? 1
                : 1ll * qpow(1ll * x * x % mod, k >> 1) * (k & 1 ? x : 1) % mod;
}
struct AP {
  int s, d, n;
  AP(int S = 0, int D = 1, int N = 0) {
    s = S;
    d = D;
    n = N;
  }
  void clear() {
    s = 0;
    d = 1;
    n = 0;
  }
  int get(int x) const { return s + x * d; }
  int last() const { return s + (n - 1) * d; }
  bool check(int x) const {
    int t = (x - s) / d;
    return (x - s) % d == 0 && t >= 0 && t < n;
  }
  bool append(int x) {
    if (n == 0)
      s = x, n = 1;
    else if (n == 1 && x > s)
      d = x - s, n = 2;
    else if (n == 2 && x > last() && (x - last()) == d)
      n++;
    else
      return false;
    return true;
  }
  AP operator+(const int &t) { return AP(s + t, d, n); }
  AP operator-(const int &t) { return AP(s - t, d, n); }
  AP operator-() { return AP(-last(), d, n); }
  AP operator&(const AP &t) const {
    if (d == t.d) {
      if (s % d != t.s % d)
        return AP();
      else {
        int l = max(s, t.s), r = min(last(), t.last());
        if (l > r)
          return AP();
        else
          return AP(l, d, (r - l) / d + 1);
      }
    } else {
      if (n <= t.n) {
        AP ret;
        for (int i = 0; i < n; i++) {
          int x = get(i);
          if (t.check(x)) ret.append(x);
        }
        return ret;
      } else
        return t & (*this);
    }
  }
};
const int maxn = 200111;
const int maxh = 20;
struct SuffixArray {
  char s[maxn];
  int n, sa[maxn], rk[maxn], b[maxn];
  int lg2[maxn], lcp[maxn], tab[maxh][maxn];
  void getsa_init() {
    int m = 256;
    for (int i = 1; i <= n; i++) b[rk[i] = s[i]]++;
    for (int i = 1; i <= m; i++) b[i] += b[i - 1];
    for (int i = n; i >= 1; i--) sa[b[rk[i]]--] = i;
  }
  void getsa(int k) {
    static int c[maxn];
    for (int i = 1; i <= n; i++)
      if (sa[i] > n - k) c[++b[rk[sa[i]]]] = sa[i];
    for (int i = 1; i <= n; i++)
      if (sa[i] > k) c[++b[rk[sa[i] - k]]] = sa[i] - k;
    swap(c, sa);
    int m = 1;
    b[1] = 0;
    c[sa[1]] = 1;
    for (int i = 2; i <= n; i++) {
      if (make_pair(rk[sa[i - 1]],
                    sa[i - 1] + k <= n ? rk[sa[i - 1] + k] : sa[i - 1]) !=
          make_pair(rk[sa[i]], sa[i] + k <= n ? rk[sa[i] + k] : sa[i]))
        b[++m] = i - 1;
      c[sa[i]] = m;
    }
    swap(c, rk);
  }
  void construct_sa() {
    getsa_init();
    for (int k = 1; k < n; k <<= 1) getsa(k);
    for (int i = 1; i <= n; i++) rk[sa[i]] = i;
  }
  void construct_lcp() {
    int h = 0;
    for (int i = 1; i <= n; i++) {
      h -= !!h;
      if (rk[i] == 1) continue;
      int j = sa[rk[i] - 1];
      while (i + h <= n && j + h <= n && s[i + h] == s[j + h]) h++;
      lcp[rk[i] - 1] = h;
    }
    for (int i = 1; i <= n; i++) tab[0][i] = lcp[i];
    for (int i = 1; i < maxh; i++) {
      for (int j = 1; j <= n - (1 << i) + 1; j++) {
        tab[i][j] = min(tab[i - 1][j], tab[i - 1][j + (1 << (i - 1))]);
      }
    }
    lg2[1] = 0;
    for (int i = 2; i <= n; i++) lg2[i] = lg2[i >> 1] + 1;
  }
  void init(char S[], int N) {
    n = N;
    for (int i = 1; i <= n; i++) s[i] = S[i];
  }
  void construct() {
    construct_sa();
    construct_lcp();
  }
  int getlcp(int x, int y) {
    x = rk[x];
    y = rk[y];
    if (x == y) return n - x + 1;
    if (x > y) swap(x, y);
    int t = lg2[y - x];
    return min(tab[t][x], tab[t][y - (1 << t)]);
  }
} S1, S2;
struct IPM {
  SuffixArray A;
  int n, m;
  struct data {
    int rk, pre, nxt, diff, dnxt;
  } tab[maxh][maxn];
  int rk[maxh][maxn], sa[maxh][maxn];
  void copysa(int k) {
    for (int i = 1; i <= n; i++) {
      data &cur = tab[k][i];
      sa[k][i] = A.sa[i];
      cur.rk = A.rk[A.sa[i]];
      cur.pre =
          (i == 1 ? 1 : (cur.rk == tab[k][i - 1].rk ? tab[k][i - 1].pre : i));
      rk[k][A.sa[i]] = i;
    }
    for (int i = n; i >= 1; i--) {
      data &cur = tab[k][i];
      if (i == n) {
        cur.diff = 1e9;
        cur.dnxt = n;
        cur.nxt = n + 1;
      } else {
        cur.diff = (cur.rk == tab[k][i + 1].rk ? sa[k][i + 1] - sa[k][i] : 1e9);
        cur.dnxt = (cur.diff == tab[k][i + 1].diff ? tab[k][i + 1].dnxt
                                                   : sa[k][i + 1]);
        cur.nxt = (cur.rk == tab[k][i + 1].rk ? tab[k][i + 1].nxt : i + 1);
      }
    }
  }
  void construct(char s[], int N) {
    n = N;
    A.init(s, n);
    A.getsa_init();
    copysa(0);
    m = 1;
    for (int k = 1; k < n; k <<= 1) {
      A.getsa(k);
      copysa(m++);
    }
  }
  AP matching(int x, int lv, int l, int r) {
    if (l > r) return AP();
    assert(r - l + 1 <= (1 << lv));
    int i = rk[lv][x];
    int pl = tab[lv][i].pre, pr = tab[lv][i].nxt;
    int p = lower_bound(sa[lv] + pl, sa[lv] + pr, l) - sa[lv];
    if (p >= pl && p < pr) {
      data &cur = tab[lv][p];
      if (sa[lv][p] > r)
        return AP();
      else
        return AP(sa[lv][p], cur.diff,
                  (min(cur.dnxt, r) - sa[lv][p]) / cur.diff + 1);
    } else
      return AP();
  }
  AP queryborder(int lv, int l, int r) {
    int L = (1 << lv);
    if (L >= r - l + 1)
      return AP(0, 1, 0);
    else {
      AP ap1 = matching(l, lv, max(l + 1, r - L * 2 + 2), r - L + 1);
      AP ap2 = matching(r - L + 1, lv, l, min(r - L, l + L - 1));
      AP ret = (ap2 & (-ap1 + (r + 1 - L + l))) + (L - l);
      return ret;
    }
  }
  vector<AP> queryborder(int l, int r) {
    vector<AP> ret;
    for (int k = 0; k < m; k++) {
      AP ap = queryborder(k, l, r);
      if (ap.n > 0) ret.push_back(ap);
    }
    return ret;
  }
  int querymxborder(int l, int r) {
    vector<AP> v = queryborder(l, r);
    for (int i = 0; i < v.size(); i++)
      if (0) cout << v[i].s << "," << v[i].d << "," << v[i].n << endl;
    if (v.size() == 0)
      return -1;
    else
      return v.back().last();
  }
} M;
int n, q;
char s[maxn];
int cnt[maxn][26];
int prep[maxn], nxtp[maxn], mnnxtp[maxn];
set<pair<int, int> > runs;
void prework() {
  for (int i = 1; i <= n; i++) {
    for (int j = 0; j < 26; j++) cnt[i][j] = cnt[i - 1][j] + (s[i] == 'a' + j);
  }
  M.construct(s, n);
  S1.init(s, n);
  S1.construct();
  reverse(s + 1, s + n + 1);
  S2.init(s, n);
  S2.construct();
  reverse(s + 1, s + n + 1);
  memset(prep, -1, sizeof(prep));
  memset(nxtp, -1, sizeof(nxtp));
  for (int i = 1; i <= n + 1; i++) mnnxtp[i] = n + 1;
  runs.clear();
  for (int L = 1; L <= n / 2; L++) {
    int R = 0;
    for (int i = 1; (i + 1) * L <= n; i++) {
      int p = i * L, q = (i + 1) * L;
      if (q <= R) continue;
      int l1 = S2.getlcp(n - p + 1, n - q + 1);
      int l2 = S1.getlcp(p, q);
      if (l1 + l2 <= L) continue;
      int l = p - l1 + 1, r = q + l2 - 1;
      if (runs.find(make_pair(l, r)) != runs.end()) continue;
      runs.insert(make_pair(l, r));
      for (int j = l; j + 2 * L - 1 <= r; j++) {
        if (nxtp[j] == -1) nxtp[j] = j + 2 * L - 1;
        if (prep[j + 2 * L - 1] == -1) prep[j + 2 * L - 1] = j;
      }
      R = r;
    }
  }
  for (int i = n; i >= 1; i--) {
    mnnxtp[i] = min(mnnxtp[i], mnnxtp[i + 1]);
    if (nxtp[i] != -1) mnnxtp[i] = min(mnnxtp[i], nxtp[i]);
  }
}
int query(int l, int r) {
  int len = r - l + 1;
  if (len <= 26) {
    bool flag = 1;
    for (int i = 0; i < 26 && flag; i++)
      flag &= (cnt[r][i] - cnt[l - 1][i]) <= 1;
    if (flag) return -1;
  }
  int mxborder = M.querymxborder(l, r);
  if (mxborder != -1 && len % (len - mxborder) == 0) return 1;
  if (nxtp[l] != -1 && nxtp[l] <= r) return 2;
  if (prep[r] != -1 && prep[r] >= l) return 2;
  if (mxborder != -1) return 2;
  if (cnt[r][s[l] - 'a'] - cnt[l - 1][s[l] - 'a'] > 1) return 3;
  if (cnt[r][s[r] - 'a'] - cnt[l - 1][s[r] - 'a'] > 1) return 3;
  if (mnnxtp[l] <= r) return 3;
  return 4;
}
int main() {
  scanf("%d", &n);
  scanf("%s", s + 1);
  prework();
  scanf("%d", &q);
  for (int i = 1; i <= q; i++) {
    int l, r;
    scanf("%d%d", &l, &r);
    printf("%d\n", query(l, r));
  }
  return 0;
}

### Prompt

In CPP, your task is to solve the following 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.

### Response

```cpp
#include <bits/stdc++.h>
using namespace std;
const int mod = 1e9 + 7;
struct fastio {
  char s[100000];
  int it, len;
  fastio() { it = len = 0; }
  inline char get() {
    if (it < len) return s[it++];
    it = 0;
    len = fread(s, 1, 100000, stdin);
    if (len == 0)
      return EOF;
    else
      return s[it++];
  }
  bool notend() {
    char c = get();
    while (c == ' ' || c == '\n') c = get();
    if (it > 0) it--;
    return c != EOF;
  }
} _buff;
inline long long getnum() {
  long long r = 0;
  bool ng = 0;
  char c;
  c = _buff.get();
  while (c != '-' && (c < '0' || c > '9')) c = _buff.get();
  if (c == '-') ng = 1, c = _buff.get();
  while (c >= '0' && c <= '9') r = r * 10 + c - '0', c = _buff.get();
  return ng ? -r : r;
}
template <class T>
inline void putnum(T x) {
  if (x < 0) putchar('-'), x = -x;
  register short a[20] = {}, sz = 0;
  while (x) a[sz++] = x % 10, x /= 10;
  if (sz == 0) putchar('0');
  for (int i = sz - 1; i >= 0; i--) putchar('0' + a[i]);
}
inline char getreal() {
  char c = _buff.get();
  while (c <= 32) c = _buff.get();
  return c;
}
long long qpow(long long x, long long k) {
  return k == 0 ? 1
                : 1ll * qpow(1ll * x * x % mod, k >> 1) * (k & 1 ? x : 1) % mod;
}
struct AP {
  int s, d, n;
  AP(int S = 0, int D = 1, int N = 0) {
    s = S;
    d = D;
    n = N;
  }
  void clear() {
    s = 0;
    d = 1;
    n = 0;
  }
  int get(int x) const { return s + x * d; }
  int last() const { return s + (n - 1) * d; }
  bool check(int x) const {
    int t = (x - s) / d;
    return (x - s) % d == 0 && t >= 0 && t < n;
  }
  bool append(int x) {
    if (n == 0)
      s = x, n = 1;
    else if (n == 1 && x > s)
      d = x - s, n = 2;
    else if (n == 2 && x > last() && (x - last()) == d)
      n++;
    else
      return false;
    return true;
  }
  AP operator+(const int &t) { return AP(s + t, d, n); }
  AP operator-(const int &t) { return AP(s - t, d, n); }
  AP operator-() { return AP(-last(), d, n); }
  AP operator&(const AP &t) const {
    if (d == t.d) {
      if (s % d != t.s % d)
        return AP();
      else {
        int l = max(s, t.s), r = min(last(), t.last());
        if (l > r)
          return AP();
        else
          return AP(l, d, (r - l) / d + 1);
      }
    } else {
      if (n <= t.n) {
        AP ret;
        for (int i = 0; i < n; i++) {
          int x = get(i);
          if (t.check(x)) ret.append(x);
        }
        return ret;
      } else
        return t & (*this);
    }
  }
};
const int maxn = 200111;
const int maxh = 20;
struct SuffixArray {
  char s[maxn];
  int n, sa[maxn], rk[maxn], b[maxn];
  int lg2[maxn], lcp[maxn], tab[maxh][maxn];
  void getsa_init() {
    int m = 256;
    for (int i = 1; i <= n; i++) b[rk[i] = s[i]]++;
    for (int i = 1; i <= m; i++) b[i] += b[i - 1];
    for (int i = n; i >= 1; i--) sa[b[rk[i]]--] = i;
  }
  void getsa(int k) {
    static int c[maxn];
    for (int i = 1; i <= n; i++)
      if (sa[i] > n - k) c[++b[rk[sa[i]]]] = sa[i];
    for (int i = 1; i <= n; i++)
      if (sa[i] > k) c[++b[rk[sa[i] - k]]] = sa[i] - k;
    swap(c, sa);
    int m = 1;
    b[1] = 0;
    c[sa[1]] = 1;
    for (int i = 2; i <= n; i++) {
      if (make_pair(rk[sa[i - 1]],
                    sa[i - 1] + k <= n ? rk[sa[i - 1] + k] : sa[i - 1]) !=
          make_pair(rk[sa[i]], sa[i] + k <= n ? rk[sa[i] + k] : sa[i]))
        b[++m] = i - 1;
      c[sa[i]] = m;
    }
    swap(c, rk);
  }
  void construct_sa() {
    getsa_init();
    for (int k = 1; k < n; k <<= 1) getsa(k);
    for (int i = 1; i <= n; i++) rk[sa[i]] = i;
  }
  void construct_lcp() {
    int h = 0;
    for (int i = 1; i <= n; i++) {
      h -= !!h;
      if (rk[i] == 1) continue;
      int j = sa[rk[i] - 1];
      while (i + h <= n && j + h <= n && s[i + h] == s[j + h]) h++;
      lcp[rk[i] - 1] = h;
    }
    for (int i = 1; i <= n; i++) tab[0][i] = lcp[i];
    for (int i = 1; i < maxh; i++) {
      for (int j = 1; j <= n - (1 << i) + 1; j++) {
        tab[i][j] = min(tab[i - 1][j], tab[i - 1][j + (1 << (i - 1))]);
      }
    }
    lg2[1] = 0;
    for (int i = 2; i <= n; i++) lg2[i] = lg2[i >> 1] + 1;
  }
  void init(char S[], int N) {
    n = N;
    for (int i = 1; i <= n; i++) s[i] = S[i];
  }
  void construct() {
    construct_sa();
    construct_lcp();
  }
  int getlcp(int x, int y) {
    x = rk[x];
    y = rk[y];
    if (x == y) return n - x + 1;
    if (x > y) swap(x, y);
    int t = lg2[y - x];
    return min(tab[t][x], tab[t][y - (1 << t)]);
  }
} S1, S2;
struct IPM {
  SuffixArray A;
  int n, m;
  struct data {
    int rk, pre, nxt, diff, dnxt;
  } tab[maxh][maxn];
  int rk[maxh][maxn], sa[maxh][maxn];
  void copysa(int k) {
    for (int i = 1; i <= n; i++) {
      data &cur = tab[k][i];
      sa[k][i] = A.sa[i];
      cur.rk = A.rk[A.sa[i]];
      cur.pre =
          (i == 1 ? 1 : (cur.rk == tab[k][i - 1].rk ? tab[k][i - 1].pre : i));
      rk[k][A.sa[i]] = i;
    }
    for (int i = n; i >= 1; i--) {
      data &cur = tab[k][i];
      if (i == n) {
        cur.diff = 1e9;
        cur.dnxt = n;
        cur.nxt = n + 1;
      } else {
        cur.diff = (cur.rk == tab[k][i + 1].rk ? sa[k][i + 1] - sa[k][i] : 1e9);
        cur.dnxt = (cur.diff == tab[k][i + 1].diff ? tab[k][i + 1].dnxt
                                                   : sa[k][i + 1]);
        cur.nxt = (cur.rk == tab[k][i + 1].rk ? tab[k][i + 1].nxt : i + 1);
      }
    }
  }
  void construct(char s[], int N) {
    n = N;
    A.init(s, n);
    A.getsa_init();
    copysa(0);
    m = 1;
    for (int k = 1; k < n; k <<= 1) {
      A.getsa(k);
      copysa(m++);
    }
  }
  AP matching(int x, int lv, int l, int r) {
    if (l > r) return AP();
    assert(r - l + 1 <= (1 << lv));
    int i = rk[lv][x];
    int pl = tab[lv][i].pre, pr = tab[lv][i].nxt;
    int p = lower_bound(sa[lv] + pl, sa[lv] + pr, l) - sa[lv];
    if (p >= pl && p < pr) {
      data &cur = tab[lv][p];
      if (sa[lv][p] > r)
        return AP();
      else
        return AP(sa[lv][p], cur.diff,
                  (min(cur.dnxt, r) - sa[lv][p]) / cur.diff + 1);
    } else
      return AP();
  }
  AP queryborder(int lv, int l, int r) {
    int L = (1 << lv);
    if (L >= r - l + 1)
      return AP(0, 1, 0);
    else {
      AP ap1 = matching(l, lv, max(l + 1, r - L * 2 + 2), r - L + 1);
      AP ap2 = matching(r - L + 1, lv, l, min(r - L, l + L - 1));
      AP ret = (ap2 & (-ap1 + (r + 1 - L + l))) + (L - l);
      return ret;
    }
  }
  vector<AP> queryborder(int l, int r) {
    vector<AP> ret;
    for (int k = 0; k < m; k++) {
      AP ap = queryborder(k, l, r);
      if (ap.n > 0) ret.push_back(ap);
    }
    return ret;
  }
  int querymxborder(int l, int r) {
    vector<AP> v = queryborder(l, r);
    for (int i = 0; i < v.size(); i++)
      if (0) cout << v[i].s << "," << v[i].d << "," << v[i].n << endl;
    if (v.size() == 0)
      return -1;
    else
      return v.back().last();
  }
} M;
int n, q;
char s[maxn];
int cnt[maxn][26];
int prep[maxn], nxtp[maxn], mnnxtp[maxn];
set<pair<int, int> > runs;
void prework() {
  for (int i = 1; i <= n; i++) {
    for (int j = 0; j < 26; j++) cnt[i][j] = cnt[i - 1][j] + (s[i] == 'a' + j);
  }
  M.construct(s, n);
  S1.init(s, n);
  S1.construct();
  reverse(s + 1, s + n + 1);
  S2.init(s, n);
  S2.construct();
  reverse(s + 1, s + n + 1);
  memset(prep, -1, sizeof(prep));
  memset(nxtp, -1, sizeof(nxtp));
  for (int i = 1; i <= n + 1; i++) mnnxtp[i] = n + 1;
  runs.clear();
  for (int L = 1; L <= n / 2; L++) {
    int R = 0;
    for (int i = 1; (i + 1) * L <= n; i++) {
      int p = i * L, q = (i + 1) * L;
      if (q <= R) continue;
      int l1 = S2.getlcp(n - p + 1, n - q + 1);
      int l2 = S1.getlcp(p, q);
      if (l1 + l2 <= L) continue;
      int l = p - l1 + 1, r = q + l2 - 1;
      if (runs.find(make_pair(l, r)) != runs.end()) continue;
      runs.insert(make_pair(l, r));
      for (int j = l; j + 2 * L - 1 <= r; j++) {
        if (nxtp[j] == -1) nxtp[j] = j + 2 * L - 1;
        if (prep[j + 2 * L - 1] == -1) prep[j + 2 * L - 1] = j;
      }
      R = r;
    }
  }
  for (int i = n; i >= 1; i--) {
    mnnxtp[i] = min(mnnxtp[i], mnnxtp[i + 1]);
    if (nxtp[i] != -1) mnnxtp[i] = min(mnnxtp[i], nxtp[i]);
  }
}
int query(int l, int r) {
  int len = r - l + 1;
  if (len <= 26) {
    bool flag = 1;
    for (int i = 0; i < 26 && flag; i++)
      flag &= (cnt[r][i] - cnt[l - 1][i]) <= 1;
    if (flag) return -1;
  }
  int mxborder = M.querymxborder(l, r);
  if (mxborder != -1 && len % (len - mxborder) == 0) return 1;
  if (nxtp[l] != -1 && nxtp[l] <= r) return 2;
  if (prep[r] != -1 && prep[r] >= l) return 2;
  if (mxborder != -1) return 2;
  if (cnt[r][s[l] - 'a'] - cnt[l - 1][s[l] - 'a'] > 1) return 3;
  if (cnt[r][s[r] - 'a'] - cnt[l - 1][s[r] - 'a'] > 1) return 3;
  if (mnnxtp[l] <= r) return 3;
  return 4;
}
int main() {
  scanf("%d", &n);
  scanf("%s", s + 1);
  prework();
  scanf("%d", &q);
  for (int i = 1; i <= q; i++) {
    int l, r;
    scanf("%d%d", &l, &r);
    printf("%d\n", query(l, r));
  }
  return 0;
}

```