#include <bits/stdc++.h>
using namespace std;
const unsigned long long base = 1000000007;
int n, q, lg[200005];
int ar[200005], al[200005], mn[200005][20];
int sum[200005][26];
char a[200005];
template <class T>
inline T Max(T a, T b) {
  return a > b ? a : b;
}
template <class T>
inline T Min(T a, T b) {
  return a < b ? a : b;
}
struct SA {
  int rk[200005], sa[200005], tp[200005], h[200005], tot[200005];
  int mn[200005][20];
  void radixsort() {
    for (int i = 0; i <= n; ++i) tot[i] = 0;
    for (int i = 1; i <= n; ++i) tot[rk[i]]++;
    for (int i = 1; i <= n; ++i) tot[i] += tot[i - 1];
    for (int i = n; i >= 1; --i) sa[tot[rk[tp[i]]]--] = tp[i];
  }
  void getsa() {
    for (int i = 1; i <= n; ++i) rk[i] = a[i] - 'a' + 1, tp[i] = i;
    radixsort();
    for (int t = 1; t < n; t <<= 1) {
      int p = 0;
      for (int i = 1; i <= t; ++i) tp[++p] = n - t + i;
      for (int i = 1; i <= n; ++i)
        if (sa[i] > t) tp[++p] = sa[i] - t;
      radixsort();
      for (int i = 1; i <= n; ++i) swap(rk[i], tp[i]);
      rk[sa[1]] = p = 1;
      for (int i = 2; i <= n; ++i)
        rk[sa[i]] =
            (tp[sa[i - 1]] == tp[sa[i]] && tp[sa[i - 1] + t] == tp[sa[i] + t])
                ? p
                : ++p;
      if (n == p) break;
    }
  }
  void geth() {
    int ht = 0;
    for (int i = 1; i <= n; ++i) rk[sa[i]] = i;
    for (int i = 1; i <= n; ++i) {
      int p = sa[rk[i] - 1];
      while (a[i + ht] == a[p + ht]) ++ht;
      h[rk[i]] = ht;
      ht = Max(0, ht - 1);
    }
  }
  int lcp(int l, int r) {
    if (l == r) return n - l + 1;
    l = rk[l];
    r = rk[r];
    if (l > r) swap(l, r);
    ++l;
    int len = lg[r - l + 1];
    return Min(mn[l][len], mn[r - (1 << len) + 1][len]);
  }
  void init() {
    getsa();
    geth();
    for (int i = 1; i <= n; ++i) mn[i][0] = h[i];
    for (int t = 1; t < 20; ++t)
      for (int i = 1; i + (1 << (t - 1)) <= n; ++i)
        mn[i][t] = Min(mn[i][t - 1], mn[i + (1 << (t - 1))][t - 1]);
  }
} sa;
int pre[200005], suf[200005];
int sig1[200005 << 2], sig2[200005 << 2];
unsigned long long qp[200005], hah[200005];
void modify(int *sig, int rt, int l, int r, int x, int y, int v) {
  if (x < 1) x = 1;
  if (y > n) y = n;
  if (x <= l && r <= y) return (void)(sig[rt] = Min(sig[rt], v));
  int mid = (l + r) >> 1;
  if (x <= mid) modify(sig, rt << 1, l, mid, x, y, v);
  if (y > mid) modify(sig, rt << 1 | 1, mid + 1, r, x, y, v);
}
void dfs1(int rt, int l, int r, int mn) {
  mn = Min(mn, sig1[rt]);
  if (l == r) return (void)(ar[l] = l + mn * 2 - 1);
  int mid = (l + r) >> 1;
  dfs1(rt << 1, l, mid, mn);
  dfs1(rt << 1 | 1, mid + 1, r, mn);
}
void dfs2(int rt, int l, int r, int mn) {
  mn = Min(mn, sig2[rt]);
  if (l == r) return (void)(al[l] = l - mn * 2 + 1);
  int mid = (l + r) >> 1;
  dfs2(rt << 1, l, mid, mn);
  dfs2(rt << 1 | 1, mid + 1, r, mn);
}
unsigned long long hsh(int l, int r) {
  return (l < 1 || r > n) ? (unsigned long long)rand() * rand()
                          : hah[r] - hah[l - 1] * qp[r - l + 1];
}
void getr() {
  srand((unsigned)time(NULL));
  qp[0] = 1;
  for (int i = 1; i < 200005; ++i) qp[i] = qp[i - 1] * base;
  memset(pre, 0, sizeof(pre));
  memset(suf, 0, sizeof(suf));
  memset(sig1, 50, sizeof(sig1));
  memset(sig2, 50, sizeof(sig2));
  for (int i = 1; i <= n; ++i) hah[i] = hah[i - 1] * base + a[i];
  for (int k = 1; k <= n; ++k) {
    for (int i = 1; i + k <= n; i += k) {
      int now = i, nxt = i + k;
      int l, r, pr = 0, sf = 0;
      l = 1;
      r = k;
      while (l <= r) {
        int mid = (l + r) >> 1;
        if (hsh(now - mid + 1, now) == hsh(nxt - mid + 1, nxt))
          l = mid + 1, pr = mid;
        else
          r = mid - 1;
      }
      l = 1;
      r = k;
      while (l <= r) {
        int mid = (l + r) >> 1;
        if (hsh(now, now + mid - 1) == hsh(nxt, nxt + mid - 1))
          l = mid + 1, sf = mid;
        else
          r = mid - 1;
      }
      int len = sf + pr - (k + 1) + 1;
      if (len <= 0) continue;
      modify(sig1, 1, 1, n, nxt - pr + 1 - k, now + sf - k, k);
      modify(sig2, 1, 1, n, nxt - pr + 1 + k - 1, now + sf + k - 1, k);
    }
  }
  dfs1(1, 1, n, 200005);
  dfs2(1, 1, n, 200005);
}
void init() {
  lg[0] = -1;
  for (int i = 1; i < 200005; ++i) lg[i] = lg[i >> 1] + 1;
  sa.init();
  for (int i = 1; i <= n; ++i) {
    for (int j = 0; j < 26; ++j) sum[i][j] = sum[i - 1][j];
    ++sum[i][a[i] - 'a'];
  }
  getr();
  for (int i = 1; i <= n; ++i) mn[i][0] = ar[i];
  for (int t = 1; t < 20; ++t)
    for (int i = 1; i + (1 << (t - 1)) <= n; ++i)
      mn[i][t] = Min(mn[i][t - 1], mn[i + (1 << (t - 1))][t - 1]);
}
int query(int l, int r) {
  int len = lg[r - l + 1];
  return Min(mn[l][len], mn[r - (1 << len) + 1][len]);
}
int border(int l, int r) {
  int len = r - l + 1, sqr = sqrt(n);
  for (int i = 1; i <= sqr && i < len; ++i)
    if (sa.lcp(l, r - i + 1) >= i) return i;
  int ret = 200005;
  for (int i = Max(1, sa.rk[l] - sqr + 1); i <= Min(n, sa.rk[l] + sqr - 1);
       ++i) {
    int loc = sa.sa[i];
    if (loc <= l || loc > r) continue;
    if (sa.lcp(l, loc) >= r - loc + 1) ret = Min(ret, r - loc + 1);
  }
  return ret;
}
int solve0(int l, int r) {
  int t[26] = {0};
  for (int i = l; i <= min(r, l + 27); ++i) ++t[a[i] - 'a'];
  for (int i = 0; i < 26; ++i)
    if (t[i] > 1) return 0;
  return 1;
}
int solve1(int l, int r) {
  int len = r - l + 1;
  for (int i = 2; i * i <= len; ++i) {
    if (len % i == 0) {
      while (len % i == 0) len /= i;
      if (sa.lcp(l, l + (r - l + 1) / i) >= r - l + 1 - (r - l + 1) / i)
        return 1;
    }
  }
  if (len != 1)
    if (sa.lcp(l, l + (r - l + 1) / len) >= r - l + 1 - (r - l + 1) / len)
      return 1;
  return 0;
}
int solve2(int l, int r) {
  if (ar[l] <= r) return 1;
  if (al[r] >= l) return 1;
  return (border(l, r) * 2 <= (r - l + 1));
}
int solve3(int l, int r) {
  if (sum[r][a[l] - 'a'] - sum[l][a[l] - 'a'] > 0) return 1;
  if (sum[r - 1][a[r] - 'a'] - sum[l - 1][a[r] - 'a'] > 0) return 1;
  return (query(l + 1, r - 1) < r);
}
int main() {
  scanf("%d%s%d", &n, a + 1, &q);
  init();
  while (q--) {
    int l, r;
    scanf("%d%d", &l, &r);
    if (solve0(l, r))
      puts("-1");
    else if (solve1(l, r))
      puts("1");
    else if (solve2(l, r))
      puts("2");
    else if (solve3(l, r))
      puts("3");
    else
      puts("4");
  }
  return 0;
}
