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>
#pragma GCC optimize("Ofast")
using namespace std;
const int N = 2e5 + 5;
const int mod1 = 1e9 + 7;
const int mod2 = 1e9 + 9;
vector<int> p[N];
int n, q;
char s[N];
long long H1[N], p1[N];
long long H2[N], p2[N];
long long get(int l, int r) {
  long long res1 = (H1[r] - H1[l - 1] * p1[r - l + 1]) % mod1;
  long long res2 = (H2[r] - H2[l - 1] * p2[r - l + 1]) % mod2;
  if (res1 < 0) res1 += mod1;
  if (res2 < 0) res2 += mod2;
  return res1 * mod2 + res2;
}
int cnt[N][26];
int count(int l, int r, int c) { return cnt[r][c] - cnt[l - 1][c]; }
unordered_map<long long, vector<int> > mp[N];
int repL[N];
int repR[N];
int rmq[N][20];
void Main_Lorentz() {
  for (int i = 2; i <= n; ++i)
    if (p[i].empty())
      for (int j = i; j <= n; j += i) p[j].push_back(i);
  for (int k = 1; k <= n; k <<= 1)
    for (int i = 1; i + k - 1 <= n; ++i) mp[k][get(i, i + k - 1)].push_back(i);
  for (int i = 1; i <= n; ++i)
    for (int k = 1; i + k - 1 <= n && !repL[i]; k <<= 1) {
      const vector<int> &v = mp[k][get(i, i + k - 1)];
      int j = lower_bound(v.begin(), v.end(), i) - v.begin() + 1;
      int r = min((int)v.size(), j + 50);
      for (; j < r && v[j] - i <= 2 * k && !repL[i]; ++j) {
        int len = v[j] - i;
        if (i + len + len >= n) break;
        if (get(i, i + len - 1) == get(i + len, i + len + len - 1))
          repL[i] = 2 * len;
      }
    }
  for (int i = 1; i <= n; ++i)
    for (int k = 1; i - k + 1 >= 1 && !repR[i]; k <<= 1) {
      const vector<int> &v = mp[k][get(i - k + 1, i)];
      int j = lower_bound(v.begin(), v.end(), i - k + 1) - v.begin() - 1;
      int l = max(-1, j - 50);
      for (; j > l && i - k + 1 - v[j] <= 2 * k && !repR[i]; --j) {
        int len = i - k + 1 - v[j];
        if (i - len - len < 0) break;
        if (get(i - len + 1, i) == get(i - len - len + 1, i - len))
          repR[i] = 2 * len;
      }
    }
  for (int i = 1; i <= n; ++i) {
    if (repL[i])
      rmq[i][0] = i - 1 + repL[i];
    else
      rmq[i][0] = n + 1;
  }
  for (int i = 0; i < 17; ++i)
    for (int j = 1; j + (2 << i) - 1 <= n; ++j)
      rmq[j][i + 1] = min(rmq[j][i], rmq[j + (1 << i)][i]);
}
bool chk0(int l, int r) {
  for (int i = 0; i < 26; ++i)
    if (count(l, r, i) > 1) return 0;
  return 1;
}
bool chk1(int l, int r) {
  for (int num : p[r - l + 1]) {
    int len = (r - l + 1) / num;
    if (get(l, r - len) == get(l + len, r)) return 1;
  }
  return 0;
}
bool chk2(int l, int r) {
  if (repL[l] && l + repL[l] - 1 <= r) return 1;
  if (repR[r] && r - repR[r] + 1 >= l) return 1;
  for (int k = 1; 2 * k <= r - l + 1; k <<= 1) {
    const vector<int> &v = mp[k][get(r - k + 1, r)];
    int i = lower_bound(v.begin(), v.end(), l) - v.begin();
    int j = lower_bound(v.begin(), v.end(), r - k + 1) - v.begin();
    int R = min(j, i + 51);
    for (int w = i; w < R; ++w) {
      int len = k + v[w] - l;
      if (2 * len > r - l + 1 || len > k + k) break;
      if (get(l, l + len - 1) == get(r - len + 1, r)) return 1;
    }
  }
  return 0;
}
double TIME() { return 1.0 * clock() / CLOCKS_PER_SEC; }
int work() {
  int L, R;
  scanf("%d%d", &L, &R);
  if (chk0(L, R)) return -1;
  if (chk1(L, R)) return 1;
  if (!count(L + 1, R, s[L]) && !count(L, R - 1, s[R])) {
    int k = log2(R - L + 1);
    return 4 - (min(rmq[L][k], rmq[R - (1 << k) + 1][k]) <= R);
  }
  return 3 - chk2(L, R);
}
int main() {
  scanf("%d%s%d", &n, s + 1, &q);
  p1[0] = p2[0] = 1;
  for (int i = 1; i <= n; ++i) {
    s[i] -= 'a';
    p1[i] = p1[i - 1] * 103 % mod1;
    p2[i] = p2[i - 1] * 101 % mod2;
    H1[i] = (H1[i - 1] * 103 + s[i]) % mod1;
    H2[i] = (H2[i - 1] * 101 + s[i]) % mod2;
    for (int j = 0; j < 26; ++j) cnt[i][j] = cnt[i - 1][j];
    cnt[i][s[i]]++;
  }
  Main_Lorentz();
  for (int i = 1; i <= q; ++i) printf("%d\n", work());
}