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 maxn = 2e5 + 10;
int n, m, Hash[2][maxn], fac[2][maxn], inv[2][maxn], num[maxn][26], cnt;
int sa[maxn], rk[maxn], f[maxn], g[maxn], st[maxn][20], Log[maxn],
    Mod[2] = {1000000007, 998244353}, seed[2] = {233, 97};
char s[maxn];
set<int> Set1, Set2;
inline int power(int a, int b, int op) {
  int r = 1;
  while (b) {
    if (b & 1) r = 1ll * r * a % Mod[op];
    a = 1ll * a * a % Mod[op];
    b >>= 1;
  }
  return r;
}
inline int Get(int op, int l, int r) {
  return ((long long)Hash[op][r] - Hash[op][l - 1] + Mod[op]) * inv[op][l - 1] %
         Mod[op];
}
inline int Find(int l, int r) {
  int x = Log[r - l + 1];
  return min(st[l][x], st[r - (1 << x) + 1][x]);
}
inline int Get1(int x, int y, int r) {
  int l = 1, pos = 0;
  while (l <= r) {
    int Mid = l + r >> 1;
    if (Get(0, x - Mid + 1, x) == Get(0, y - Mid + 1, y) &&
        Get(1, x - Mid + 1, x) == Get(1, y - Mid + 1, y)) {
      pos = Mid;
      l = Mid + 1;
    } else
      r = Mid - 1;
  }
  return pos;
}
inline int Get2(int x, int y, int r) {
  int l = 1, pos = 0;
  r = min(r, n - y + 1);
  while (l <= r) {
    int Mid = l + r >> 1;
    if (Get(0, x, x + Mid - 1) == Get(0, y, y + Mid - 1) &&
        Get(1, x, x + Mid - 1) == Get(1, y, y + Mid - 1)) {
      pos = Mid;
      l = Mid + 1;
    } else
      r = Mid - 1;
  }
  return pos;
}
inline int Solve(int l, int r) {
  int len = n;
  for (int i = (1), _end_ = (sqrt(len)); i <= _end_; ++i)
    if (i < r - l + 1 && Get(0, l, l + i - 1) == Get(0, r - i + 1, r) &&
        Get(1, l, l + i - 1) == Get(1, r - i + 1, r))
      return i;
  int Min = n + 1;
  for (int t = (rk[l] - sqrt(len)), _end_ = (rk[l] + sqrt(len)); t <= _end_;
       ++t)
    if (1 <= t && t <= n) {
      int i = sa[t];
      if (l < i && i <= r && Get(0, l, l + r - i) == Get(0, i, r) &&
          Get(1, l, l + r - i) == Get(1, i, r))
        Min = min(Min, r - i + 1);
    }
  return Min == n + 1 ? -1 : Min;
}
inline void SA() {
  int x[maxn], y[maxn], c[maxn], m = 25;
  for (int i = (1), _end_ = (n); i <= _end_; ++i) {
    x[i] = s[i] - 'a';
    ++c[x[i]];
  }
  for (int i = (1), _end_ = (m); i <= _end_; ++i) c[i] += c[i - 1];
  for (int i = n; i >= 1; --i) sa[c[x[i]]--] = i;
  for (int k = 1; k <= n; k <<= 1) {
    memset((c), 0, sizeof(c));
    int tot = 0;
    for (int i = (n - k + 1), _end_ = (n); i <= _end_; ++i) y[++tot] = i;
    for (int i = (1), _end_ = (n); i <= _end_; ++i)
      if (sa[i] > k) y[++tot] = sa[i] - k;
    for (int i = (1), _end_ = (n); i <= _end_; ++i) ++c[x[y[i]]];
    for (int i = (1), _end_ = (m); i <= _end_; ++i) c[i] += c[i - 1];
    for (int i = n; i >= 1; --i) sa[c[x[y[i]]]--] = y[i];
    swap(x, y);
    x[sa[1]] = tot = 1;
    for (int i = (2), _end_ = (n); i <= _end_; ++i)
      x[sa[i]] = (y[sa[i]] == y[sa[i - 1]] && y[sa[i] + k] == y[sa[i - 1] + k])
                     ? tot
                     : ++tot;
    m = tot;
  }
  for (int i = (1), _end_ = (n); i <= _end_; ++i) rk[sa[i]] = i;
}
inline void Modify1(int l, int r, int val) {
  if (l > r) return;
  auto it = Set1.lower_bound(l);
  while (it != Set1.end() && *it <= r) {
    f[*it] = val;
    auto nxt = it;
    ++nxt;
    Set1.erase(it);
    it = nxt;
  }
}
inline void Modify2(int l, int r, int val) {
  if (l > r) return;
  auto it = Set2.lower_bound(l);
  while (it != Set2.end() && *it <= r) {
    g[*it] = val;
    auto nxt = it;
    ++nxt;
    Set2.erase(it);
    it = nxt;
  }
}
signed main() {
  scanf("%d", &n);
  scanf("%s", s + 1);
  SA();
  for (int op = (0), _end_ = (1); op <= _end_; ++op) {
    fac[op][0] = inv[op][0] = 1;
    for (int i = (1), _end_ = (n); i <= _end_; ++i) {
      fac[op][i] = 1ll * fac[op][i - 1] * seed[op] % Mod[op];
      inv[op][i] =
          1ll * inv[op][i - 1] * power(seed[op], Mod[op] - 2, op) % Mod[op];
    }
    for (int i = (1), _end_ = (n); i <= _end_; ++i) {
      Hash[op][i] =
          (Hash[op][i - 1] + 1ll * fac[op][i] * (s[i] - 'a')) % Mod[op];
      for (int j = (0), _end_ = (25); j <= _end_; ++j)
        num[i][j] = num[i - 1][j];
      ++num[i][s[i] - 'a'];
    }
  }
  for (int i = (1), _end_ = (n); i <= _end_; ++i) {
    f[i] = g[i] = n + 1;
    Set1.insert(i);
    Set2.insert(i);
  }
  for (int len = (1), _end_ = (n); len <= _end_; ++len)
    for (int i = len; i + len < n; i += len) {
      int x = Get1(i, i + len, len), y = Get2(i + 1, i + len + 1, len);
      Modify1(i - x + 1, i - len + y + 1, len);
      Modify2(i + len + len - x, i + len + y, len);
    }
  for (int i = (1), _end_ = (n); i <= _end_; ++i)
    st[i][0] = i + f[i] + f[i] - 1;
  for (int j = (1), _end_ = (18); j <= _end_; ++j)
    for (int i = (1), _end_ = (n); i <= _end_; ++i)
      st[i][j] = min(st[i][j - 1], st[i + (1 << j - 1)][j - 1]);
  for (int i = (2), _end_ = (n); i <= _end_; ++i) Log[i] = Log[i >> 1] + 1;
  scanf("%d", &m);
  cnt = 0;
  while (m--) {
    int l, r;
    scanf("%d%d", &l, &r);
    int len = r - l + 1;
    ++cnt;
    for (int i = 1; i * i <= len; ++i) {
      if (i != len && len % i == 0 &&
          Get(0, l, l + (len - i) - 1) == Get(0, r - (len - i) + 1, r) &&
          Get(1, l, l + (len - i) - 1) == Get(1, r - (len - i) + 1, r)) {
        puts("1");
        goto Next;
      }
      if (i != 1 && len % i == 0 &&
          Get(0, l, l + (len - len / i) - 1) ==
              Get(0, r - (len - len / i) + 1, r) &&
          Get(1, l, l + (len - len / i) - 1) ==
              Get(1, r - (len - len / i) + 1, r)) {
        puts("1");
        goto Next;
      }
    }
    if (l + f[l] + f[l] - 1 <= r) {
      puts("2");
      continue;
    }
    if (r - g[r] - g[r] + 1 >= l) {
      puts("2");
      continue;
    }
    if (Solve(l, r) != -1) {
      puts("2");
      continue;
    }
    if (num[r][s[r] - 'a'] - num[l - 1][s[r] - 'a'] >= 2) {
      puts("3");
      continue;
    }
    if (num[r][s[l] - 'a'] - num[l - 1][s[l] - 'a'] >= 2) {
      puts("3");
      continue;
    }
    if (Find(l, r) <= r) {
      puts("3");
      continue;
    }
    for (int j = (0), _end_ = (25); j <= _end_; ++j)
      if (num[r][j] - num[l - 1][j] >= 2) {
        puts("4");
        goto Next;
      }
    puts("-1");
  Next:;
  }
  return 0;
}