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 = 200005, S = 18;
int n, t, q, m = 221, p, sum[N][26], b = 121;
int L[N], R[N], v[N];
int sa[N], cnt[N], rk[N], oldrk[N << 1], id[N], height[N];
multiset<int> A, B;
vector<int> insL[N], insR[N], delL[N], delR[N];
int st[N][S], Log[N];
char s[N];
unsigned long long H[N], P[N];
int inline queryST(int l, int r) {
  if (l > r) swap(l, r);
  ++l;
  int k = Log[r - l + 1];
  return min(st[l][k], st[r - (1 << k) + 1][k]);
}
unsigned long long inline queryHash(int l, int r) {
  return H[r] - H[l - 1] * P[r - l + 1];
}
bool inline check1(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;
      int o = (r - l + 1) / i;
      if (queryST(rk[l], rk[l + o]) >= r - l + 1 - o) return true;
    }
  }
  if (len != 1) {
    int o = (r - l + 1) / len;
    if (queryST(rk[l], rk[l + o]) >= r - l + 1 - o) return true;
  }
  return false;
}
bool inline check2(int l, int r) {
  int len = r - l + 1;
  if (L[l] && L[l] * 2 <= len) return true;
  if (R[r] && R[r] * 2 <= len) return true;
  for (int i = 1; i <= min(t, len / 2); i++)
    if (queryHash(l, l + i - 1) == queryHash(r - i + 1, r)) return true;
  int L = max(1, rk[l] - t - 1);
  int R = min(n, rk[l] + t + 1);
  int v = height[rk[l]];
  for (int i = rk[l] - 1; i >= L; i--) {
    if (sa[i] > l && sa[i] <= r && sa[i] + v - 1 >= r) return true;
    v = min(v, height[i]);
  }
  v = 2e9;
  for (int i = rk[l] + 1; i <= R; i++) {
    v = min(v, height[i]);
    if (sa[i] > l && sa[i] <= r && sa[i] + v - 1 >= r) return true;
  }
  return false;
}
bool inline check3(int l, int r) {
  if (v[r] >= l) return true;
  if (sum[r][s[r] - 'a'] - sum[l - 1][s[r] - 'a'] > 1) return true;
  if (sum[r][s[l] - 'a'] - sum[l - 1][s[l] - 'a'] > 1) return true;
  return false;
}
bool inline check4(int l, int r) {
  for (int i = 0; i < 26; i++)
    if (sum[r][i] - sum[l - 1][i] > 1) return true;
  return false;
}
bool inline cmp(int i, int j, int k) {
  return oldrk[i] == oldrk[j] && oldrk[i + k] == oldrk[j + k];
}
void inline SA() {
  for (int i = 1; i <= n; i++) cnt[rk[i] = s[i]]++;
  for (int i = 1; i <= m; i++) cnt[i] += cnt[i - 1];
  for (int i = n; i; i--) sa[cnt[rk[i]]--] = i;
  for (int w = 1; w < n; w <<= 1, m = p) {
    p = 0;
    for (int i = n; i > n - w; i--) id[++p] = i;
    for (int i = 1; i <= n; i++)
      if (sa[i] > w) id[++p] = sa[i] - w;
    for (int i = 0; i <= m; i++) cnt[i] = 0;
    for (int i = 1; i <= n; i++) cnt[rk[i]]++, oldrk[i] = rk[i];
    for (int i = 1; i <= m; i++) cnt[i] += cnt[i - 1];
    for (int i = n; i; i--) sa[cnt[rk[id[i]]]--] = id[i];
    p = 0;
    for (int i = 1; i <= n; i++) rk[sa[i]] = cmp(sa[i], sa[i - 1], w) ? p : ++p;
  }
  for (int i = 1; i <= n; i++) {
    int j = sa[rk[i] - 1], k = max(0, height[rk[i - 1]] - 1);
    while (s[i + k] == s[j + k]) k++;
    height[rk[i]] = k;
  }
  for (int i = 1; i <= n; i++) st[i][0] = height[i];
  for (int j = 1; j < S; j++) {
    for (int i = 1; i + (1 << j) - 1 <= n; i++) {
      st[i][j] = min(st[i][j - 1], st[i + (1 << (j - 1))][j - 1]);
    }
  }
}
int inline queryLt(int a, int b) {
  int l = 1, r = a;
  while (l < r) {
    int mid = (l + r + 1) >> 1;
    if (queryHash(a - mid + 1, a) == queryHash(b - mid + 1, b))
      l = mid;
    else
      r = mid - 1;
  }
  return r;
}
void inline prework() {
  for (int w = 1; w <= n; w++) {
    for (int b = 2 * w; b <= n; b += w) {
      int a = b - w;
      if (s[a] == s[b]) {
        int Rt = min(queryST(rk[a], rk[b]), w);
        int Lt = min(queryLt(a, b), w);
        int La = a - Lt + 1, Ra = a + Rt - 1 - w + 1;
        if (La <= Ra) {
          insL[La].push_back(w);
          delL[Ra + 1].push_back(w);
          insR[La + 2 * w - 1].push_back(w);
          delR[Ra + 2 * w].push_back(w);
        }
      }
    }
  }
  for (int i = 1; i <= n; i++) {
    for (int j = 0; j < insL[i].size(); j++) A.insert(insL[i][j]);
    for (int j = 0; j < insR[i].size(); j++) B.insert(insR[i][j]);
    for (int j = 0; j < delL[i].size(); j++) A.erase(A.find(delL[i][j]));
    for (int j = 0; j < delR[i].size(); j++) B.erase(B.find(delR[i][j]));
    if (!A.empty()) L[i] = *A.begin();
    if (!B.empty()) R[i] = *B.begin();
  }
  for (int i = 1; i <= n; i++)
    v[i] = max(v[i - 1], R[i] ? (i - 2 * R[i] + 1) : 0);
}
int main() {
  scanf("%d%s%d", &n, s + 1, &q);
  for (int i = 1; i <= n; i++) Log[i] = log2(i);
  P[0] = 1, t = sqrt(n);
  for (int i = 1; i <= n; i++) {
    P[i] = P[i - 1] * b;
    H[i] = H[i - 1] * b + s[i];
    for (int j = 0; j < 26; j++) sum[i][j] = sum[i - 1][j];
    sum[i][s[i] - 'a']++;
  }
  SA();
  prework();
  while (q--) {
    int l, r;
    scanf("%d%d", &l, &r);
    if (check1(l, r))
      puts("1");
    else if (check2(l, r))
      puts("2");
    else if (check3(l, r))
      puts("3");
    else if (check4(l, r))
      puts("4");
    else
      puts("-1");
  }
  return 0;
}