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 N = 2e5 + 5;
int n, q, f[18][N], g[N], lg[N], sum[26][N];
char str[N];
struct suffix_array {
  int t[N], x[N], y[N], sa[N], rk[N], hei[18][N];
  bool cmp(int i, int j, int k) { return y[i] == y[j] && y[i + k] == y[j + k]; }
  void build() {
    int m = 30;
    for (int i = 1; i <= n; ++i) ++t[x[i] = str[i] - 'a' + 1];
    for (int i = 1; i <= m; ++i) t[i] += t[i - 1];
    for (int i = n; i; --i) sa[t[x[i]]--] = i;
    for (int k = 1; k <= n; k <<= 1) {
      int p = 0;
      for (int i = 0; i <= m; ++i) y[i] = 0;
      for (int i = n - k + 1; i <= n; ++i) y[++p] = i;
      for (int i = 1; i <= n; ++i)
        if (sa[i] > k) y[++p] = sa[i] - k;
      for (int i = 0; i <= m; ++i) t[i] = 0;
      for (int i = 1; i <= n; ++i) ++t[x[y[i]]];
      for (int i = 1; i <= m; ++i) t[i] += t[i - 1];
      for (int i = n; i; --i) sa[t[x[y[i]]]--] = y[i];
      swap(x, y);
      x[sa[1]] = p = 1;
      for (int i = 2; i <= n; ++i)
        x[sa[i]] = cmp(sa[i], sa[i - 1], k) ? p : ++p;
      if (p >= n) break;
      m = p;
    }
    for (int i = 1; i <= n; ++i) rk[sa[i]] = i;
    for (int i = 1, j = 0; i <= n; ++i) {
      if (j) --j;
      while (str[i + j] == str[sa[rk[i] - 1] + j]) ++j;
      hei[0][rk[i]] = j;
    }
    for (int i = 1; i <= lg[n]; ++i)
      for (int j = 1; j + (1 << i) - 1 <= n; ++j)
        hei[i][j] = min(hei[i - 1][j], hei[i - 1][j + (1 << i - 1)]);
  }
  int lcp(int i, int j) {
    if (i == j) return n - i + 1;
    i = rk[i];
    j = rk[j];
    if (i > j) swap(i, j);
    ++i;
    int k = lg[j - i + 1];
    return min(hei[k][i], hei[k][j - (1 << k) + 1]);
  }
  bool equal(int l1, int r1, int l2, int r2) {
    return lcp(l1, l2) >= r1 - l1 + 1;
  }
  int getminborder(int l, int r) {
    int res = r - l + 1;
    for (int i = 1; i <= r - l + 1 && i <= 450; ++i)
      if (equal(l, l + i - 1, r - i + 1, r)) res = min(res, i);
    for (int i = max(rk[l] - 450, 1); i <= min(rk[l] + 450, n); ++i)
      if (sa[i] >= l && sa[i] <= r && equal(l, l + r - sa[i], sa[i], r))
        res = min(res, r - sa[i] + 1);
    return res;
  }
} A, B;
struct segment_tree {
  int tag[N << 2];
  void build(int x, int l, int r) {
    tag[x] = 1 << 30;
    if (l == r) return;
    int mid = l + r >> 1;
    build(x << 1, l, mid);
    build(x << 1 | 1, mid + 1, r);
  }
  void modify(int x, int l, int r, int ql, int qr, int v) {
    if (l >= ql && r <= qr) {
      tag[x] = min(tag[x], v);
      return;
    }
    int mid = l + r >> 1;
    if (ql <= mid) modify(x << 1, l, mid, ql, qr, v);
    if (qr > mid) modify(x << 1 | 1, mid + 1, r, ql, qr, v);
  }
  void print(int x, int l, int r, int v, int *arr) {
    v = min(v, tag[x]);
    if (l == r) {
      arr[l] = v;
      return;
    }
    int mid = l + r >> 1;
    print(x << 1, l, mid, v, arr);
    print(x << 1 | 1, mid + 1, r, v, arr);
  }
} L, R;
bool check_negative_1(int l, int r) {
  if (r - l + 1 > 26) return 0;
  static int t[26];
  for (int i = 0; i < 26; ++i) t[i] = 0;
  for (int i = l; i <= r; ++i) {
    if (t[str[i] - 'a']) return 0;
    t[str[i] - 'a'] = 1;
  }
  return 1;
}
bool check_1(int l, int r) {
  if (A.equal(l, r - 1, l + 1, r)) return 1;
  int len = r - l + 1;
  for (int i = 2; i * i <= len; ++i)
    if (len % i == 0) {
      if (A.equal(l, r - i, l + i, r)) return 1;
      if (A.equal(l, r - len / i, l + len / i, r)) return 1;
    }
  return 0;
}
bool check_2(int l, int r) {
  return f[0][l] <= r || g[r] >= l || A.getminborder(l, r) <= r - l + 1 >> 1;
}
bool find(int l, int r, int k) { return sum[k][r] > sum[k][l - 1]; }
int rmq(int l, int r) {
  int k = lg[r - l + 1];
  return min(f[k][l], f[k][r - (1 << k) + 1]);
}
bool check_3(int l, int r) {
  return find(l, r - 1, str[r] - 'a') || find(l + 1, r, str[l] - 'a') ||
         rmq(l, r) <= r;
}
int query(int l, int r) {
  if (check_negative_1(l, r)) return -1;
  if (check_1(l, r)) return 1;
  if (check_2(l, r)) return 2;
  if (check_3(l, r)) return 3;
  return 4;
}
int main() {
  scanf("%d%s%d", &n, str + 1, &q);
  lg[0] = -1;
  for (int i = 2; i <= n; ++i) lg[i] = lg[i >> 1] + 1;
  A.build();
  reverse(str + 1, str + n + 1);
  B.build();
  reverse(str + 1, str + n + 1);
  L.build(1, 1, n);
  R.build(1, 1, n);
  for (int len = 1; len + len <= n; ++len)
    for (int i = len, j = len + len; j <= n; i += len, j += len) {
      int a = B.lcp(n - i + 1, n - j + 1), b = A.lcp(i, j);
      if (a + b - 1 >= len) {
        L.modify(1, 1, n, i - a + 1, i + b - len, len << 1);
        R.modify(1, 1, n, j - a + len, j + b - 1, len << 1);
      }
    }
  L.print(1, 1, n, 1 << 30, f[0]);
  R.print(1, 1, n, 1 << 30, g);
  for (int i = 1; i <= n; ++i) f[0][i] = i + f[0][i] - 1, g[i] = i - g[i] + 1;
  for (int i = 1; i <= lg[n]; ++i)
    for (int j = 1; j + (1 << i) - 1 <= n; ++j)
      f[i][j] = min(f[i - 1][j], f[i - 1][j + (1 << i - 1)]);
  for (int i = 0; i < 26; ++i)
    for (int j = 1; j <= n; ++j)
      sum[i][j] = sum[i][j - 1] + (str[j] == i + 'a');
  for (int i = 1, l, r; i <= q; ++i) {
    scanf("%d%d", &l, &r);
    printf("%d\n", query(l, r));
  }
  return 0;
}