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 base = 1000000007;
int n, q, lg[200005];
int ar[200005], al[200005], mn[200005][20];
int sum[200005][26];
char a[200005];
template <class T>
inline T Max(T a, T b) {
  return a > b ? a : b;
}
template <class T>
inline T Min(T a, T b) {
  return a < b ? a : b;
}
struct SA {
  int rk[200005], sa[200005], tp[200005], h[200005], tot[200005];
  int mn[200005][20];
  void radixsort() {
    for (int i = 0; i <= n; ++i) tot[i] = 0;
    for (int i = 1; i <= n; ++i) tot[rk[i]]++;
    for (int i = 1; i <= n; ++i) tot[i] += tot[i - 1];
    for (int i = n; i >= 1; --i) sa[tot[rk[tp[i]]]--] = tp[i];
  }
  void getsa() {
    for (int i = 1; i <= n; ++i) rk[i] = a[i] - 'a' + 1, tp[i] = i;
    radixsort();
    for (int t = 1; t < n; t <<= 1) {
      int p = 0;
      for (int i = 1; i <= t; ++i) tp[++p] = n - t + i;
      for (int i = 1; i <= n; ++i)
        if (sa[i] > t) tp[++p] = sa[i] - t;
      radixsort();
      for (int i = 1; i <= n; ++i) swap(rk[i], tp[i]);
      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] + t] == tp[sa[i] + t])
                ? p
                : ++p;
      if (n == p) break;
    }
  }
  void geth() {
    int ht = 0;
    for (int i = 1; i <= n; ++i) rk[sa[i]] = i;
    for (int i = 1; i <= n; ++i) {
      int p = sa[rk[i] - 1];
      while (a[i + ht] == a[p + ht]) ++ht;
      h[rk[i]] = ht;
      ht = Max(0, ht - 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 len = lg[r - l + 1];
    return Min(mn[l][len], mn[r - (1 << len) + 1][len]);
  }
  void init() {
    getsa();
    geth();
    for (int i = 1; i <= n; ++i) mn[i][0] = h[i];
    for (int t = 1; t < 20; ++t)
      for (int i = 1; i + (1 << (t - 1)) <= n; ++i)
        mn[i][t] = Min(mn[i][t - 1], mn[i + (1 << (t - 1))][t - 1]);
  }
} sa;
int pre[200005], suf[200005];
int sig1[200005 << 2], sig2[200005 << 2];
unsigned long long qp[200005], hah[200005];
void modify(int *sig, int rt, int l, int r, int x, int y, int v) {
  if (x < 1) x = 1;
  if (y > n) y = n;
  if (x <= l && r <= y) return (void)(sig[rt] = Min(sig[rt], v));
  int mid = (l + r) >> 1;
  if (x <= mid) modify(sig, rt << 1, l, mid, x, y, v);
  if (y > mid) modify(sig, rt << 1 | 1, mid + 1, r, x, y, v);
}
void dfs1(int rt, int l, int r, int mn) {
  mn = Min(mn, sig1[rt]);
  if (l == r) return (void)(ar[l] = l + mn * 2 - 1);
  int mid = (l + r) >> 1;
  dfs1(rt << 1, l, mid, mn);
  dfs1(rt << 1 | 1, mid + 1, r, mn);
}
void dfs2(int rt, int l, int r, int mn) {
  mn = Min(mn, sig2[rt]);
  if (l == r) return (void)(al[l] = l - mn * 2 + 1);
  int mid = (l + r) >> 1;
  dfs2(rt << 1, l, mid, mn);
  dfs2(rt << 1 | 1, mid + 1, r, mn);
}
unsigned long long hsh(int l, int r) {
  return (l < 1 || r > n) ? (unsigned long long)rand() * rand()
                          : hah[r] - hah[l - 1] * qp[r - l + 1];
}
void getr() {
  srand((unsigned)time(NULL));
  qp[0] = 1;
  for (int i = 1; i < 200005; ++i) qp[i] = qp[i - 1] * base;
  memset(pre, 0, sizeof(pre));
  memset(suf, 0, sizeof(suf));
  memset(sig1, 50, sizeof(sig1));
  memset(sig2, 50, sizeof(sig2));
  for (int i = 1; i <= n; ++i) hah[i] = hah[i - 1] * base + a[i];
  for (int k = 1; k <= n; ++k) {
    for (int i = 1; i + k <= n; i += k) {
      int now = i, nxt = i + k;
      int l, r, pr = 0, sf = 0;
      l = 1;
      r = k;
      while (l <= r) {
        int mid = (l + r) >> 1;
        if (hsh(now - mid + 1, now) == hsh(nxt - mid + 1, nxt))
          l = mid + 1, pr = mid;
        else
          r = mid - 1;
      }
      l = 1;
      r = k;
      while (l <= r) {
        int mid = (l + r) >> 1;
        if (hsh(now, now + mid - 1) == hsh(nxt, nxt + mid - 1))
          l = mid + 1, sf = mid;
        else
          r = mid - 1;
      }
      int len = sf + pr - (k + 1) + 1;
      if (len <= 0) continue;
      modify(sig1, 1, 1, n, nxt - pr + 1 - k, now + sf - k, k);
      modify(sig2, 1, 1, n, nxt - pr + 1 + k - 1, now + sf + k - 1, k);
    }
  }
  dfs1(1, 1, n, 200005);
  dfs2(1, 1, n, 200005);
}
void init() {
  lg[0] = -1;
  for (int i = 1; i < 200005; ++i) lg[i] = lg[i >> 1] + 1;
  sa.init();
  for (int i = 1; i <= n; ++i) {
    for (int j = 0; j < 26; ++j) sum[i][j] = sum[i - 1][j];
    ++sum[i][a[i] - 'a'];
  }
  getr();
  for (int i = 1; i <= n; ++i) mn[i][0] = ar[i];
  for (int t = 1; t < 20; ++t)
    for (int i = 1; i + (1 << (t - 1)) <= n; ++i)
      mn[i][t] = Min(mn[i][t - 1], mn[i + (1 << (t - 1))][t - 1]);
}
int query(int l, int r) {
  int len = lg[r - l + 1];
  return Min(mn[l][len], mn[r - (1 << len) + 1][len]);
}
int border(int l, int r) {
  int len = r - l + 1, sqr = sqrt(n);
  for (int i = 1; i <= sqr && i < len; ++i)
    if (sa.lcp(l, r - i + 1) >= i) return i;
  int ret = 200005;
  for (int i = Max(1, sa.rk[l] - sqr + 1); i <= Min(n, sa.rk[l] + sqr - 1);
       ++i) {
    int loc = sa.sa[i];
    if (loc <= l || loc > r) continue;
    if (sa.lcp(l, loc) >= r - loc + 1) ret = Min(ret, r - loc + 1);
  }
  return ret;
}
int solve0(int l, int r) {
  int t[26] = {0};
  for (int i = l; i <= min(r, l + 27); ++i) ++t[a[i] - 'a'];
  for (int i = 0; i < 26; ++i)
    if (t[i] > 1) return 0;
  return 1;
}
int solve1(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 (sa.lcp(l, l + (r - l + 1) / i) >= r - l + 1 - (r - l + 1) / i)
        return 1;
    }
  }
  if (len != 1)
    if (sa.lcp(l, l + (r - l + 1) / len) >= r - l + 1 - (r - l + 1) / len)
      return 1;
  return 0;
}
int solve2(int l, int r) {
  if (ar[l] <= r) return 1;
  if (al[r] >= l) return 1;
  return (border(l, r) * 2 <= (r - l + 1));
}
int solve3(int l, int r) {
  if (sum[r][a[l] - 'a'] - sum[l][a[l] - 'a'] > 0) return 1;
  if (sum[r - 1][a[r] - 'a'] - sum[l - 1][a[r] - 'a'] > 0) return 1;
  return (query(l + 1, r - 1) < r);
}
int main() {
  scanf("%d%s%d", &n, a + 1, &q);
  init();
  while (q--) {
    int l, r;
    scanf("%d%d", &l, &r);
    if (solve0(l, r))
      puts("-1");
    else if (solve1(l, r))
      puts("1");
    else if (solve2(l, r))
      puts("2");
    else if (solve3(l, r))
      puts("3");
    else
      puts("4");
  }
  return 0;
}