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 = 2147483647;
char s[200010];
int n, sq;
namespace ex {
char s[200010], t[200010];
int nex[200010], ex[200010], n, m;
void getnex() {
  int i, a, p;
  m = strlen(t + 1);
  memset(nex, 0, (m + 1) << 2);
  a = p = 0;
  nex[1] = m;
  for (i = 2; i <= m; i++) {
    if (i + nex[i - a + 1] >= p) {
      for (p = max(p, i); p <= m && t[p] == t[p - i + 1]; p++)
        ;
      nex[i] = p - i;
      a = i;
    } else
      nex[i] = nex[i - a + 1];
  }
}
void getex() {
  int i, a, p;
  getnex();
  n = strlen(s + 1);
  a = p = 0;
  for (i = 1; i <= n; i++) {
    if (i + nex[i - a + 1] >= p) {
      for (p = max(p, i); p <= n && p - i + 1 <= m && s[p] == t[p - i + 1]; p++)
        ;
      ex[i] = p - i;
      a = i;
    } else
      ex[i] = nex[i - a + 1];
  }
}
}  // namespace ex
int* po;
struct seg {
  int mn[800010];
  void build(int l, int r, int x) {
    mn[x] = inf;
    if (l == r) return;
    int mid = (l + r) >> 1;
    build(l, mid, x << 1);
    build(mid + 1, r, x << 1 | 1);
  }
  void fmin(int x, int v) {
    if (v < mn[x]) mn[x] = v;
  }
  void pushdown(int x) {
    if (mn[x] != inf) {
      fmin(x << 1, mn[x]);
      fmin(x << 1 | 1, mn[x]);
      mn[x] = inf;
    }
  }
  void modify(int L, int R, int v, int l = 1, int r = n, int x = 1) {
    if (L <= l && r <= R) return fmin(x, v);
    pushdown(x);
    int mid = (l + r) >> 1;
    if (L <= mid) modify(L, R, v, l, mid, x << 1);
    if (mid < R) modify(L, R, v, mid + 1, r, x << 1 | 1);
  }
  void dfs(int l, int r, int x) {
    if (l == r) {
      po[l] = mn[x];
      return;
    }
    pushdown(x);
    int mid = (l + r) >> 1;
    dfs(l, mid, x << 1);
    dfs(mid + 1, r, x << 1 | 1);
  }
} sl, sr;
namespace mainlo {
int ls[200010], lp[200010], pos;
void newright(char* a, int n, char* b, int m, bool f) {
  memcpy(ex::t + 1, b + 1, m);
  ex::t[m + 1] = 0;
  ex::getnex();
  memcpy(lp, ex::nex, (m + 1) << 2);
  lp[m + 1] = 0;
  reverse(a + 1, a + n + 1);
  reverse(b + 1, b + m + 1);
  memcpy(ex::s + 1, b + 1, m);
  ex::s[m + 1] = 0;
  memcpy(ex::t + 1, a + 1, n);
  ex::t[n + 1] = 0;
  ex::getex();
  memcpy(ls, ex::ex, (m + 1) << 2);
  reverse(ls + 1, ls + m + 1);
  int i, st, en;
  for (i = 1; i <= m; i++) {
    st = 2 * i - ls[i];
    en = min(2 * i - 1, i + lp[i + 1]);
    if (st <= en) {
      if (f) {
        sl.modify(pos + st - 2 * i + 1, pos + en - 2 * i + 1, i * 2);
        sr.modify(pos + st, pos + en, i * 2);
      } else {
        sl.modify(pos - en, pos - st, i * 2);
        sr.modify(pos - en + i * 2 - 1, pos - st + i * 2 - 1, i * 2);
      }
    }
  }
}
void solve(int l, int r) {
  if (l == r) return;
  int mid = (l + r) >> 1, ln = mid - l + 1, rn = r - mid;
  pos = mid;
  newright(s + l - 1, ln, s + mid, rn, 1);
  pos = mid + 1;
  newright(s + mid, rn, s + l - 1, ln, 0);
  solve(l, mid);
  solve(mid + 1, r);
}
}  // namespace mainlo
namespace sa {
int rk[400010], sa[200010], h[200010], c[200010], mn[200010][18], lg[200010];
struct pr {
  int c[2], i;
  pr(int a = 0, int b = 0, int d = 0) : c{a, b}, i(d) {}
} p[200010], q[200010];
bool operator!=(pr a, pr b) { return a.c[0] != b.c[0] || a.c[1] != b.c[1]; }
void sort(int f) {
  int m, i;
  memset(c, 0, sizeof(c));
  for (i = 1, m = 0; i <= n; i++) {
    c[p[i].c[f]]++;
    m = max(m, p[i].c[f]);
  }
  for (i = 1; i <= m; i++) c[i] += c[i - 1];
  for (i = n; i > 0; i--) q[c[p[i].c[f]]--] = p[i];
  memcpy(p, q, sizeof(q));
}
void suf() {
  int i, j, l, m;
  for (i = 1; i <= n; i++) rk[i] = s[i] - 'a' + 1;
  for (l = 1; l <= n; l <<= 1) {
    for (i = 1; i <= n; i++) p[i] = pr(rk[i], rk[i + l], i);
    sort(1);
    sort(0);
    for (i = 1, m = 0; i <= n; i++) {
      if (p[i] != p[i - 1]) m++;
      rk[p[i].i] = m;
    }
  }
  for (i = 1; i <= n; i++) sa[rk[i]] = i;
  for (i = 1, l = 0; i <= n; i++) {
    if (l) l--;
    while (s[i + l] == s[sa[rk[i] - 1] + l]) l++;
    h[rk[i]] = l;
  }
  for (i = 1; i <= n; i++) mn[i][0] = h[i];
  for (j = 1; j < 18; j++) {
    for (i = 1; i + (1 << j) - 1 <= n; i++)
      mn[i][j] = min(mn[i][j - 1], mn[i + (1 << (j - 1))][j - 1]);
  }
  for (i = 2; i <= n; i++) lg[i] = lg[i >> 1] + 1;
}
int query(int l, int r) {
  int k = lg[r - l + 1];
  return min(mn[l][k], mn[r - (1 << k) + 1][k]);
}
int lcp(int x, int y) {
  if (rk[x] > rk[y]) swap(x, y);
  return query(rk[x] + 1, rk[y]);
}
int border(int l, int r) {
  int i, s = inf;
  for (i = 1; i <= sq && i < r - l + 1; i++) {
    if (lcp(l, r - i + 1) >= i) return i;
  }
  for (i = max(1, rk[l] - sq + 1); i <= min(n, rk[l] + sq - 1); i++) {
    if (l < sa[i] && sa[i] <= r && lcp(l, sa[i]) >= r - sa[i] + 1)
      s = min(s, r - sa[i] + 1);
  }
  return s == inf ? inf / 2 : s;
}
}  // namespace sa
int lt[200010], rt[200010];
bool vis[30];
bool checkno(int l, int r) {
  if (r - l + 1 > 26) return 0;
  memset(vis, 0, sizeof(vis));
  for (int i = l; i <= r; i++) {
    if (vis[s[i] - 'a']) return 0;
    vis[s[i] - 'a'] = 1;
  }
  return 1;
}
vector<int> pd[200010];
bool check1(int l, int r) {
  for (int& t : pd[r - l + 1]) {
    if (sa::lcp(l, l + (r - l + 1) / t) >= r - l + 1 - (r - l + 1) / t)
      return 1;
  }
  return 0;
}
bool check2(int l, int r) {
  return lt[l] <= r || rt[r] >= l || sa::border(l, r) * 2 <= r - l + 1;
}
int sum[200010][26];
int get(int l, int r, int c) { return sum[r][c - 'a'] - sum[l - 1][c - 'a']; }
int mn[200010][18], lg[200010];
int qmin(int l, int r) {
  int k = lg[r - l + 1];
  return min(mn[l][k], mn[r - (1 << k) + 1][k]);
}
bool check3(int l, int r) {
  return get(l + 1, r, s[l]) || get(l, r - 1, s[r]) || qmin(l, r) <= r;
}
int query(int l, int r) {
  if (checkno(l, r)) return -1;
  if (check1(l, r)) return 1;
  if (check2(l, r)) return 2;
  if (check3(l, r)) return 3;
  return 4;
}
int main() {
  int m, i, j, l, r;
  scanf("%d%s", &n, s + 1);
  sq = sqrt(n);
  sl.build(1, n, 1);
  sr.build(1, n, 1);
  mainlo::solve(1, n);
  po = lt;
  sl.dfs(1, n, 1);
  po = rt;
  sr.dfs(1, n, 1);
  sa::suf();
  for (i = 1; i <= n; i++) lt[i] = lt[i] == inf ? n + 1 : i + lt[i] - 1;
  for (i = 1; i <= n; i++) rt[i] = rt[i] == inf ? 0 : i - rt[i] + 1;
  for (i = 2; i <= n; i++) {
    if (pd[i].empty()) {
      for (j = i; j <= n; j += i) pd[j].push_back(i);
    }
  }
  for (i = 1; i <= n; i++) {
    memcpy(sum[i], sum[i - 1], sizeof(sum[i - 1]));
    sum[i][s[i] - 'a']++;
  }
  for (i = 1; i <= n; i++) mn[i][0] = lt[i];
  for (j = 1; j < 18; j++) {
    for (i = 1; i + (1 << j) - 1 <= n; i++)
      mn[i][j] = min(mn[i][j - 1], mn[i + (1 << (j - 1))][j - 1]);
  }
  for (i = 2; i <= n; i++) lg[i] = lg[i >> 1] + 1;
  scanf("%d", &m);
  while (m--) {
    scanf("%d%d", &l, &r);
    printf("%d\n", query(l, r));
  }
}