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 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;
}