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(2)
using namespace std;
const int M = 200005;
int read() {
  int x = 0, f = 1;
  char c;
  while ((c = getchar()) < '0' || c > '9') {
    if (c == '-') f = -1;
  }
  while (c >= '0' && c <= '9') {
    x = (x << 3) + (x << 1) + (c ^ 48);
    c = getchar();
  }
  return x * f;
}
int n, q, S, cnt;
char s[M];
int lt[M], rt[M], lg[M], fa[M][2], dp[M][20], pre[M][26];
unsigned long long hs[M], pw[M];
unsigned long long get(int l, int r) {
  if (l < 1 || r > n) return rand() * rand();
  return hs[r] - hs[l - 1] * pw[r - l + 1];
}
int find(int t, int x) {
  if (x != fa[x][t]) fa[x][t] = find(t, fa[x][t]);
  return fa[x][t];
}
int ask(int l, int r) {
  int k = lg[r - l + 1];
  return min(dp[l][k], dp[r - (1 << k) + 1][k]);
}
void init1() {
  pw[0] = 1;
  fa[n + 1][0] = fa[n + 1][1] = n + 1;
  for (int i = 1; i <= n; i++) {
    for (int j = 0; j < 26; j++) pre[i][j] = pre[i - 1][j];
    pre[i][s[i] - 'a']++;
  }
  for (int i = 1; i <= n; i++) {
    lt[i] = n + 1;
    rt[i] = 0;
    hs[i] = 371 * hs[i - 1] + s[i];
    pw[i] = pw[i - 1] * 371;
    fa[i][0] = fa[i][1] = i;
  }
  for (int k = 1; k <= n; k++)
    for (int i = 1; i + k <= n; i += k) {
      int j = i + k, lcp = 0, lcs = 0, l = 1, r = k;
      while (l <= r) {
        int mid = (l + r) >> 1;
        if (get(i - mid + 1, i) == get(j - mid + 1, j))
          lcs = mid, l = mid + 1;
        else
          r = mid - 1;
      }
      l = 1;
      r = k;
      while (l <= r) {
        int mid = (l + r) >> 1;
        if (get(i, i + mid - 1) == get(j, j + mid - 1))
          lcp = mid, l = mid + 1;
        else
          r = mid - 1;
      }
      if (lcs + lcp < k) continue;
      l = max(1, i - lcs + 1);
      r = min(i + lcp - k, n);
      for (int i = find(0, l); i <= r; i = find(0, i)) {
        lt[i] = i + 2 * k;
        fa[i][0] = find(0, i + 1);
        i++;
        cnt++;
      }
      l = max(1, j + k - lcs);
      r = min(j + lcp - 1, n);
      for (int i = find(1, l); i <= r; i = find(1, i)) {
        rt[i] = i - 2 * k;
        fa[i][1] = find(1, i + 1);
        i++;
        cnt++;
      }
    }
  for (int i = 1; i <= n; i++) {
    dp[i][0] = lt[i];
    if (i > 1) lg[i] = lg[i >> 1] + 1;
  }
  for (int j = 1; (1 << j) <= n; j++)
    for (int i = 1; i + (1 << j) - 1 <= n; i++)
      dp[i][j] = min(dp[i][j - 1], dp[i + (1 << j - 1)][j - 1]);
}
int x[M], y[M], c[M], sa[M], rk[M], hi[M][20];
void init2() {
  int m = 200;
  for (int i = 1; i <= n; i++) ++c[x[i] = s[i]];
  for (int i = 1; i <= m; 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) {
    int num = 0;
    for (int i = n - k + 1; i <= n; i++) y[++num] = i;
    for (int i = 1; i <= n; i++)
      if (sa[i] > k) y[++num] = sa[i] - k;
    for (int i = 1; i <= m; i++) c[i] = 0;
    for (int i = 1; i <= n; i++) ++c[x[i]];
    for (int i = 1; i <= m; 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]] = num = 1;
    for (int i = 2; i <= n; i++)
      x[sa[i]] = (y[sa[i]] == y[sa[i - 1]] && y[sa[i] + k] == y[sa[i - 1] + k])
                     ? num
                     : ++num;
    if (m == n) break;
    m = num;
  }
  int k = 0;
  for (int i = 1; i <= n; i++) rk[sa[i]] = i;
  for (int i = 1; i <= n; i++) {
    if (rk[i] == 1) continue;
    if (k) k--;
    int j = sa[rk[i] - 1];
    while (i + k <= n && j + k <= n && s[i + k] == s[j + k]) k++;
    hi[rk[i]][0] = k;
  }
  for (int j = 1; (1 << j) <= n; j++)
    for (int i = 1; i + (1 << j) - 1 <= n; i++)
      hi[i][j] = min(hi[i][j - 1], hi[i + (1 << j - 1)][j - 1]);
}
int lcp(int l, int r) {
  l = rk[l];
  r = rk[r];
  if (l > r) swap(l, r);
  l++;
  int k = lg[r - l + 1];
  return min(hi[l][k], hi[r - (1 << k) + 1][k]);
}
int s0(int l, int r) {
  int t[26] = {};
  for (int i = l; i <= min(l + 27, r); i++) t[s[i] - 'a']++;
  for (int i = 0; i < 26; i++)
    if (t[i] > 1) return 0;
  return 1;
}
int s1(int l, int r) {
  int x = r - l + 1, y = x;
  for (int i = 2; i * i <= x; i++) {
    if (x % i == 0) {
      while (x % i == 0) x /= i;
      if (lcp(l, l + y / i) >= y - y / i) return 1;
    }
  }
  if (x != 1) return lcp(l, l + y / x) >= y - y / x;
  return 0;
}
int s2(int l, int r) {
  if (lt[l] <= r || rt[r] >= l) return 1;
  for (int i = 1; i <= min(S, r - l); i++)
    if (get(l, l + i - 1) == get(r - i + 1, r)) return 1;
  int p = rk[l];
  for (int t = max(1, p - S); t <= min(n, p + S); t++) {
    int i = sa[t];
    if (l < i && i <= r && lcp(l, i) >= r - i + 1) return 1;
  }
  return 0;
}
int s3(int l, int r) {
  if (pre[r][s[l] - 'a'] - pre[l][s[l] - 'a'] > 0) return 1;
  if (pre[r - 1][s[r] - 'a'] - pre[l - 1][s[r] - 'a'] > 0) return 1;
  return ask(l, r) <= r;
}
signed main() {
  n = read();
  S = sqrt(n);
  scanf("%s", s + 1);
  init1();
  init2();
  q = read();
  while (q--) {
    int l = read(), r = read();
    if (s0(l, r))
      puts("-1");
    else if (s1(l, r))
      puts("1");
    else if (s2(l, r))
      puts("2");
    else if (s3(l, r))
      puts("3");
    else
      puts("4");
  }
}