#include <bits/stdc++.h>
using namespace std;
void read(int &x) {
  char ch = getchar();
  x = 0;
  while (ch < '0' || ch > '9') ch = getchar();
  while (ch >= '0' && ch <= '9')
    x = (x << 1) + (x << 3) + ch - 48, ch = getchar();
}
char ch[200000 + 1];
int n;
struct HASH {
  int h[200000 + 1], mi[200000 + 1], inv[200000 + 1], p, mo;
  int pw(int x, int k) {
    int res = 1;
    while (k) {
      if (k & 1) res = 1ll * res * x % mo;
      x = 1ll * x * x % mo;
      k >>= 1;
    }
    return res;
  }
  void build(int x, int y) {
    mo = x, p = y;
    mi[0] = 1;
    for (int i = 1, end_i = n; i <= end_i; i++)
      mi[i] = 1ll * mi[i - 1] * p % mo;
    inv[n] = pw(mi[n], mo - 2);
    for (int i = n, end_i = 1; i >= end_i; i--)
      inv[i - 1] = 1ll * inv[i] * p % mo;
    for (int i = 1, end_i = n; i <= end_i; i++)
      h[i] = (h[i - 1] + 1ll * (ch[i] - 'a' + 1) * mi[i] % mo) % mo;
  }
  int hash(int l, int r) {
    return 1ll * (h[r] - h[l - 1] + mo) % mo * inv[l] % mo;
  }
} h1, h2;
bool check(int l, int r, int x, int y) {
  return h1.hash(l, r) == h1.hash(x, y) && h2.hash(l, r) == h2.hash(x, y);
}
int Log[200000 + 1];
struct SA {
  int sa[200000 + 1], rk[200000 + 1], ht[200000 + 1], id[200000 + 1],
      oldrk[200000 << 1], buc[200000 + 1], px[200000 + 1],
      ft[200000 + 1][18 + 1];
  int sk;
  void mysort() {
    fill(buc, buc + 1 + sk, 0);
    for (int i = 1, end_i = n; i <= end_i; i++) ++buc[px[i] = rk[id[i]]];
    for (int i = 1, end_i = sk; i <= end_i; i++) buc[i] += buc[i - 1];
    for (int i = n, end_i = 1; i >= end_i; i--) sa[buc[px[i]]--] = id[i];
  }
  bool pd(int x, int y, int z) {
    return oldrk[x] == oldrk[y] && oldrk[x + z] == oldrk[y + z];
  }
  void build(char ch[]) {
    memset(rk, 0, sizeof rk), memset(oldrk, 0, sizeof oldrk);
    sk = 26;
    for (int i = 1, end_i = n; i <= end_i; i++) rk[id[i] = i] = ch[i] - 'a' + 1;
    mysort();
    for (int w = 1, p = 0; w <= n; w <<= 1, p = 0) {
      for (int i = n - w + 1, end_i = n; i <= end_i; i++) id[++p] = i;
      for (int i = 1, end_i = n; i <= end_i; i++)
        if (sa[i] > w) id[++p] = sa[i] - w;
      mysort();
      memcpy(oldrk, rk, sizeof rk);
      sk = 0;
      for (int i = 1, end_i = n; i <= end_i; i++)
        rk[sa[i]] = pd(sa[i], sa[i - 1], w) ? sk : ++sk;
      if (sk == n) {
        for (int i = 1, end_i = n; i <= end_i; i++) sa[rk[i]] = i;
        break;
      }
    }
    sk = 0;
    for (int i = 1, end_i = n; i <= end_i; i++) {
      if (sk) --sk;
      while (ch[i + sk] == ch[sa[rk[i] - 1] + sk]) ++sk;
      ht[rk[i]] = sk;
    }
    for (int i = 1, end_i = n; i <= end_i; i++) ft[i][0] = ht[i];
    for (int j = 0, end_j = 18 - 1; j <= end_j; j++)
      for (int i = 1, end_i = n; i <= end_i; i++)
        ft[i][j + 1] =
            (i + (1 << j) <= n) ? min(ft[i][j], ft[i + (1 << j)][j]) : ft[i][j];
  }
  int dt;
  int get(int l, int r) {
    l = rk[l], r = rk[r];
    if (l > r) swap(l, r);
    if (++l == r) return ft[l][0];
    dt = Log[r - l + 1];
    return min(ft[l][dt], ft[r - (1 << dt) + 1][dt]);
  }
} s1, s2;
struct Tree {
  int vl[200000 << 2];
  Tree() { memset(vl, 0x3f, sizeof vl); }
  void Add(int t, int l, int r, int x, int y, int k, int lz) {
    if (lz < vl[t]) vl[t] = lz;
    if (x <= l && r <= y) {
      vl[t] = k;
      return;
    }
    int mid = l + r >> 1;
    if (x <= mid) Add((t << 1), l, mid, x, y, k, vl[t]);
    if (y > mid) Add((t << 1 | 1), mid + 1, r, x, y, k, vl[t]);
  }
  void fil(int t, int l, int r, int lz, int at[]) {
    if (lz < vl[t]) vl[t] = lz;
    if (l == r) {
      at[l] = vl[t];
      return;
    }
    int mid = l + r >> 1;
    fil((t << 1), l, mid, vl[t], at), fil((t << 1 | 1), mid + 1, r, vl[t], at);
  }
} t1, t2;
int gt[200000 + 1][18 + 1], dl[200000 + 1], dr[200000 + 1], sc[200000 + 1][26];
const int inf = 0x3f3f3f3f;
int sqn;
void Init() {
  Log[1] = 0;
  for (int i = 2, end_i = n; i <= end_i; i++) Log[i] = Log[i >> 1] + 1;
  h1.build(998244353, 37);
  h2.build(1000000007, 29);
  s1.build(ch);
  for (int i = (n >> 1), end_i = 1; i >= end_i; i--) swap(ch[i], ch[n - i + 1]);
  s2.build(ch);
  for (int i = (n >> 1), end_i = 1; i >= end_i; i--) swap(ch[i], ch[n - i + 1]);
  int l, r, lcp, lcs, dlen, pl, pr;
  for (int len = (n >> 1), end_len = 1; len >= end_len; len--) {
    for (int k = (n / len) - 1, end_k = 1; k >= end_k; k--) {
      l = len * k, r = len * (k + 1);
      lcs = s1.get(l, r);
      lcp = s2.get(n - l + 1, n - r + 1);
      if (lcp + lcs > len) {
        lcp = min(lcp, len), lcs = min(lcs, len);
        dlen = lcp + lcs - len - 1;
        t1.Add(1, 1, n, l - lcp + 1, l - lcp + 1 + dlen, (len << 1), inf);
        t2.Add(1, 1, n, r + lcs - 1 - dlen, r + lcs - 1, (len << 1), inf);
      }
    }
  }
  t1.fil(1, 1, n, inf, dl), t2.fil(1, 1, n, inf, dr);
  for (int i = 1, end_i = n; i <= end_i; i++) {
    for (int j = 0, end_j = 25; j <= end_j; j++) sc[i][j] = sc[i - 1][j];
    ++sc[i][ch[i] - 'a'];
  }
  for (int i = 1, end_i = n; i <= end_i; i++) gt[i][0] = i + dl[i] - 1;
  for (int j = 0, end_j = 18 - 1; j <= end_j; j++)
    for (int i = 1, end_i = n; i <= end_i; i++)
      gt[i][j + 1] =
          (i + (1 << j) <= n) ? min(gt[i][j], gt[i + (1 << j)][j]) : gt[i][j];
  sqn = sqrt(n);
}
bool pd0(int l, int r) {
  for (int i = 0, end_i = 25; i <= end_i; i++)
    if (sc[r][i] - sc[l - 1][i] > 1) return 0;
  return 1;
}
int len, sq;
bool pd1(int l, int r) {
  len = r - l + 1, sq = sqrt(len);
  if (check(l, r - 1, l + 1, r)) return 1;
  for (int i = 2, end_i = sq; i <= end_i; i++)
    if (len % i == 0)
      if (check(l, r - i, l + i, r) || check(l, r - len / i, l + len / i, r))
        return 1;
  return 0;
}
int min_ht;
bool pd2(int l, int r) {
  len = r - l + 1;
  if (dl[l] <= len || dr[r] <= len) return 1;
  for (int i = min(sqn, (len >> 1)), end_i = 1; i >= end_i; i--)
    if (check(l, l + i - 1, r - i + 1, r)) return 1;
  if (sqn >= (len >> 1)) return 0;
  min_ht = s1.ht[s1.rk[l]];
  for (int i = s1.rk[l] - 1, end_i = s1.rk[l] - sqn + 1; i >= end_i; i--) {
    if (i < 1) break;
    if ((l + r >> 1) < s1.sa[i] && s1.sa[i] <= r && s1.sa[i] + min_ht > r)
      return 1;
    min_ht = min(min_ht, s1.ht[i]);
  }
  min_ht = len;
  for (int i = s1.rk[l] + 1, end_i = s1.rk[l] + sqn - 1; i <= end_i; i++) {
    if (i > n) break;
    min_ht = min(min_ht, s1.ht[i]);
    if ((l + r >> 1) < s1.sa[i] && s1.sa[i] <= r && s1.sa[i] + min_ht > r)
      return 1;
  }
  return 0;
}
bool pd3(int l, int r) {
  return (sc[r][ch[l] - 'a'] - sc[l][ch[l] - 'a'] > 0) ||
         (sc[r - 1][ch[r] - 'a'] - sc[l - 1][ch[r] - 'a'] > 0);
}
int dx;
int get_Min_dl(int l, int r) {
  if (l == r) return gt[l][0];
  dx = Log[r - l + 1];
  return min(gt[l][dx], gt[r - (1 << dx) + 1][dx]);
}
int main() {
  scanf("%d\n%s", &n, ch + 1);
  Init();
  int T, l, r;
  read(T);
  while (T--) {
    read(l), read(r);
    if (pd0(l, r))
      puts("-1");
    else if (pd1(l, r))
      puts("1");
    else if (pd2(l, r))
      puts("2");
    else if (pd3(l, r) || get_Min_dl(l, r) <= r)
      puts("3");
    else
      puts("4");
  }
  return 0;
}
