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>
template <typename _Tp>
void read(_Tp &x) {
  char ch(getchar());
  bool f(false);
  while (!isdigit(ch)) f |= ch == 45, ch = getchar();
  x = ch & 15, ch = getchar();
  while (isdigit(ch)) x = x * 10 + (ch & 15), ch = getchar();
  if (f) x = -x;
}
template <typename _Tp, typename... Args>
void read(_Tp &t, Args &...args) {
  read(t);
  read(args...);
}
int read_str(char *s) {
  char ch(getchar());
  while (ch == ' ' || ch == '\r' || ch == '\n') ch = getchar();
  char *tar = s;
  *tar++ = ch, ch = getchar();
  while (ch != ' ' && ch != '\r' && ch != '\n' && ch != EOF)
    *tar++ = ch, ch = getchar();
  return *tar = 0, tar - s;
}
template <typename _Tp>
inline void chmax(_Tp &a, const _Tp &b) {
  a = a < b ? b : a;
}
template <typename _Tp>
inline void chmin(_Tp &a, const _Tp &b) {
  a = a < b ? a : b;
}
const int N = 200005;
char s[N], t[N];
int n, m, Log2[N];
struct rmq {
  int f[N][20];
  inline int ask(int l, int r) {
    int k = Log2[r - l + 1];
    return std::min(f[l][k], f[r - (1 << k) + 1][k]);
  }
  void init(int *a) {
    for (int i = n; i >= 1; --i) {
      f[i][0] = a[i];
      for (int j = 1; j < 20 && i + (1 << (j - 1)) <= n; ++j)
        f[i][j] = std::min(f[i][j - 1], f[i + (1 << (j - 1))][j - 1]);
    }
  }
};
struct DS {
  int sa[N], rk[N], tax[N], tp[N], H[N];
  rmq _;
  void Rsort() {
    memset(tax, 0, (m + 3) << 2);
    for (int i = 1; i <= n; ++i) ++tax[rk[i]];
    for (int i = 1; i <= m; ++i) tax[i] += tax[i - 1];
    for (int i = n; i >= 1; --i) sa[tax[rk[tp[i]]]--] = tp[i];
  }
  void SA(char *s) {
    for (int i = 1; i <= n; ++i) rk[i] = s[i], tp[i] = i;
    m = 127, Rsort();
    for (int w = 1, p = 0; p < n; w <<= 1, m = p) {
      p = 0;
      for (int i = 0; i < w; ++i) tp[++p] = n - i;
      for (int i = 1; i <= n; ++i)
        if (sa[i] > w) tp[++p] = sa[i] - w;
      Rsort(), memcpy(tp, rk, (n + 3) << 2), 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] + w] == tp[sa[i] + w]
                ? p
                : ++p;
    }
    for (int i = 1, k = 0; i <= n; ++i) {
      if (rk[i] == 1) continue;
      if (k) --k;
      for (int j = sa[rk[i] - 1]; s[i + k] == s[j + k]; ++k)
        ;
      H[rk[i]] = k;
    }
    _.init(H);
  }
  inline int ask(int l, int r) {
    l = rk[l], r = rk[r];
    if (l > r) std::swap(l, r);
    return _.ask(l + 1, r);
  }
} a, b;
inline int lcp(int x, int y) { return a.ask(x, y); }
inline int lcs(int x, int y) { return b.ask(n - x + 1, n - y + 1); }
struct seg {
  int mn[524288 + 5], M;
  void build(int n) {
    for (M = 1; M <= n + 2; M <<= 1)
      ;
    memset(mn, 63, sizeof(mn));
  }
  void upd(int s, int t, int C) {
    if (s > t) return;
    for (s += M - 1, t += M + 1; s ^ t ^ 1; s >>= 1, t >>= 1) {
      if (~s & 1) chmin(mn[s ^ 1], C);
      if (t & 1) chmin(mn[t ^ 1], C);
    }
  }
  void out(int *a, int op) {
    for (int i = 2; i <= n + M; ++i) chmin(mn[i], mn[i >> 1]);
    for (int i = 1; i <= n; ++i) a[i] = i + op * (2 * mn[i + M] - 1);
  }
} c, d;
int sl[N], sr[N], cnt[26], B, tr[N], tl[N], last[26];
rmq _;
bool issol(int l, int r) {
  if (r - l + 1 > 26) return true;
  memset(cnt, 0, sizeof(cnt));
  for (int i = l; i <= r; ++i) ++cnt[s[i] - 97];
  return *std::max_element(cnt, cnt + 26) > 1;
}
bool check1(int l, int r) {
  int len = r - l + 1, x = len;
  for (int i = 2; i * i <= x; ++i)
    if (x % i == 0) {
      while (x % i == 0) x /= i;
      if (lcp(l, l + len / i) >= len - len / i) return true;
    }
  if (x > 1) return lcp(l, l + len / x) >= len - len / x;
  return false;
}
bool check2(int l, int r) {
  if (sl[l] <= r || sr[r] >= l) return true;
  int len = r - l + 1;
  for (int i = 1; i <= B && i + i <= len; ++i)
    if (lcp(l, r - i + 1) >= i) return true;
  for (int _ = std::max(1, a.rk[l] - B), ed = std::min(n, a.rk[l] + B); _ <= ed;
       ++_) {
    int i = a.sa[_];
    if (l < i && i <= r && r - i + 1 > B && lcp(l, i) >= r - i + 1) return true;
  }
  return false;
}
inline bool check3(int l, int r) {
  return tr[l] <= r || tl[r] >= l || _.ask(l, r) <= r;
}
int main() {
  for (int i = 2; i < N; ++i) Log2[i] = Log2[i >> 1] + 1;
  read(n), read_str(s + 1);
  for (int i = 1; i <= n; ++i) t[n - i + 1] = s[i];
  a.SA(s), b.SA(t), c.build(n), d.build(n);
  for (int len = n / 2; len >= 1; --len) {
    for (int x = 1, y = len + 1; y <= n; x += len, y += len)
      if (s[x] == s[y]) {
        int l1 = std::min(len, lcp(x, y)), l2 = std::min(len, lcs(x, y));
        c.upd(x - l2 + 1, x + l1 - len, len),
            d.upd(y - l2 + len, y + l1 - 1, len);
      }
  }
  c.out(sl, 1), d.out(sr, -1), _.init(sl);
  memset(last, 0, sizeof(last));
  for (int i = 1; i <= n; ++i) tl[i] = last[s[i] - 97], last[s[i] - 97] = i;
  memset(last, 63, sizeof(last));
  for (int i = n; i >= 1; --i) tr[i] = last[s[i] - 97], last[s[i] - 97] = i;
  int q, l, r;
  read(q);
  for (B = 1; B * B <= n; ++B)
    ;
  while (q--) {
    read(l, r);
    if (!issol(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;
}