#include <bits/stdc++.h>
using namespace std;
const unsigned long long P = 23333;
const int inf = 1 << 30;
char s[200002];
int n, q, i, j, L[200002], R[200002], st[200002][22], lg[200002],
    sum[200002][26];
unsigned long long h[200002], pw[200002];
struct SA {
  int a[200002], cntx[200002], cnty[200002], sa[200002], tsa[200002],
      rk[200002], h[200002], x[200002], y[200002], st[200002][22];
  void build() {
    for (int i = 1; i <= n; i++) a[i] = (int)s[i];
    for (int i = 1; i <= n; i++) cntx[a[i]]++;
    for (int i = 2; i <= 256; i++) cntx[i] += cntx[i - 1];
    for (int i = n; i >= 1; i--) sa[cntx[a[i]]--] = i;
    rk[sa[1]] = 1;
    for (int i = 2; i <= n; i++) {
      rk[sa[i]] = rk[sa[i - 1]];
      if (a[sa[i]] != a[sa[i - 1]]) rk[sa[i]]++;
    }
    for (int j = 1; rk[sa[n]] < n; j <<= 1) {
      for (int i = 0; i <= n; i++) cntx[i] = cnty[i] = 0;
      for (int i = 1; i <= n; i++) {
        x[i] = rk[i];
        y[i] = (i + j <= n) ? rk[i + j] : 0;
        cntx[x[i]]++;
        cnty[y[i]]++;
      }
      for (int i = 1; i <= n; i++)
        cntx[i] += cntx[i - 1], cnty[i] += cnty[i - 1];
      for (int i = n; i >= 1; i--) tsa[cnty[y[i]]--] = i;
      for (int i = n; i >= 1; i--) sa[cntx[x[tsa[i]]]--] = tsa[i];
      rk[sa[1]] = 1;
      for (int i = 2; i <= n; i++) {
        rk[sa[i]] = rk[sa[i - 1]];
        if (x[sa[i]] != x[sa[i - 1]] || y[sa[i]] != y[sa[i - 1]]) rk[sa[i]]++;
      }
    }
    for (int i = 1, j = 0; i <= n; i++) {
      if (j) j--;
      while (a[sa[rk[i]] + j] == a[sa[rk[i] - 1] + j] && sa[rk[i]] + j <= n)
        j++;
      h[rk[i]] = j;
    }
    for (int i = 1; i <= n; i++) st[i][0] = h[i];
    for (int j = 1; j <= 20; j++) {
      for (int i = 1; i + (1 << (j - 1)) <= n; i++)
        st[i][j] = min(st[i][j - 1], st[i + (1 << (j - 1))][j - 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 k = lg[r - l + 1];
    return min(st[l][k], st[r - (1 << k) + 1][k]);
  }
  int border(int l, int r) {
    int len = r - l + 1, q = sqrt(n), ans = n;
    for (int i = 1; i <= q && i < len; i++) {
      if (lcp(l, r - i + 1) >= i) return i;
    }
    for (int i = max(1, rk[l] - q + 1); i <= min(n, rk[l] + q - 1); i++) {
      int pos = sa[i];
      if (pos <= l || pos > r) continue;
      if (lcp(l, pos) >= r - pos + 1) ans = min(ans, r - pos + 1);
    }
    return ans;
  }
} A;
struct SegmentTree {
  struct node {
    int dat, add;
    node() { dat = add = inf; }
  } t[200002 * 4];
  void modify(int p, int x) {
    t[p].dat = min(t[p].dat, x);
    t[p].add = min(t[p].add, x);
  }
  void spread(int p) {
    if (t[p].add != inf) {
      modify(p * 2, t[p].add);
      modify(p * 2 + 1, t[p].add);
      t[p].add = inf;
    }
  }
  void change(int p, int l, int r, int ql, int qr, int x) {
    if (ql < 1) ql = 1;
    if (qr > n) qr = n;
    if (ql <= l && r <= qr) {
      modify(p, x);
      return;
    }
    int mid = (l + r) / 2;
    spread(p);
    if (ql <= mid) change(p * 2, l, mid, ql, qr, x);
    if (qr > mid) change(p * 2 + 1, mid + 1, r, ql, qr, x);
    t[p].dat = min(t[p * 2].dat, t[p * 2 + 1].dat);
  }
  void dfs(int p, int l, int r, int op) {
    if (l == r) {
      if (op == 1)
        R[l] = (t[p].dat == inf) ? n + 1 : l + 2 * t[p].dat - 1;
      else
        L[l] = (t[p].dat == inf) ? 0 : l - 2 * t[p].dat + 1;
      return;
    }
    int mid = (l + r) / 2;
    spread(p);
    dfs(p * 2, l, mid, op);
    dfs(p * 2 + 1, mid + 1, r, op);
  }
} t1, t2;
unsigned long long cal(int l, int r) {
  if (l < 1 || r > n) return (unsigned long long)rand() * rand();
  return h[r] - h[l - 1] * pw[r - l + 1];
}
int ask(int l, int r) {
  int k = lg[r - l + 1];
  return min(st[l][k], st[r - (1 << k) + 1][k]);
}
bool check0(int l, int r) {
  int flag = 0, c[26] = {0};
  for (int i = l; i <= min(r, l + 26); i++) c[(int)(s[i] - 'a')]++;
  for (int i = 0; i < 26; i++) {
    if (c[i] > 1) flag = 1;
  }
  return (flag != 1);
}
bool check1(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 (A.lcp(l, l + (r - l + 1) / i) >= r - l + 1 - (r - l + 1) / i)
        return 1;
    }
  }
  if (len != 1) {
    if (A.lcp(l, l + (r - l + 1) / len) >= r - l + 1 - (r - l + 1) / len)
      return 1;
  }
  return 0;
}
bool check2(int l, int r) {
  if (R[l] <= r || L[r] >= l || A.border(l, r) * 2 <= r - l + 1) return 1;
  return 0;
}
bool check3(int l, int r) {
  int pl = (int)(s[l] - 'a'), pr = (int)(s[r] - 'a');
  if (sum[r][pl] - sum[l][pl] != 0 || sum[r - 1][pr] - sum[l - 1][pr] != 0)
    return 1;
  else if (ask(l, r) <= r)
    return 1;
  else
    return 0;
}
int main() {
  srand(time(0));
  scanf("%d%s", &n, s + 1);
  A.build();
  for (i = 1; i <= n; i++) {
    for (j = 0; j < 26; j++) sum[i][j] = sum[i - 1][j];
    sum[i][(int)(s[i] - 'a')]++;
  }
  for (i = 1; i <= n; i++) h[i] = h[i - 1] * P + (unsigned long long)s[i];
  for (i = pw[0] = 1; i <= n; i++) pw[i] = pw[i - 1] * P;
  for (i = 2; i <= n; i++) lg[i] = lg[i >> 1] + 1;
  for (i = 1; i <= n; i++) {
    for (j = 1; j + i <= n; j += i) {
      int x = j, y = j + i, l = 1, r = i, mid, lcs = 0, lcp = 0;
      while (l <= r) {
        mid = (l + r) / 2;
        if (cal(x - mid + 1, x) == cal(y - mid + 1, y))
          lcs = mid, l = mid + 1;
        else
          r = mid - 1;
      }
      l = 1;
      r = i;
      while (l <= r) {
        mid = (l + r) / 2;
        if (cal(x, x + mid - 1) == cal(y, y + mid - 1))
          lcp = mid, l = mid + 1;
        else
          r = mid - 1;
      }
      if (lcs + lcp > i) {
        t1.change(1, 1, n, x - lcs + 1, x + lcp - i, i);
        t2.change(1, 1, n, y - lcs + 1 + i - 1, x + lcp + i - 1, i);
      }
    }
  }
  t1.dfs(1, 1, n, 1);
  t2.dfs(1, 1, n, 2);
  for (i = 1; i <= n; i++) st[i][0] = R[i];
  for (j = 1; j <= 20; j++) {
    for (i = 1; i + (1 << (j - 1)) <= n; i++)
      st[i][j] = min(st[i][j - 1], st[i + (1 << (j - 1))][j - 1]);
  }
  scanf("%d", &q);
  for (i = 1; i <= q; i++) {
    int l, r;
    scanf("%d%d", &l, &r);
    if (check0(l, r))
      puts("-1");
    else if (check1(l, r))
      puts("1");
    else if (check2(l, r))
      puts("2");
    else if (check3(l, r))
      puts("3");
    else
      puts("4");
  }
  return 0;
}
