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 INF = 1e9;
const int N = 200010, C = 26;
int n, sqrtn;
char str[N];
int csum[N][C];
namespace SA {
int n;
int reala[N], realb[N], *a = reala, *b = realb;
int sa[N], rk[N], h[N];
int c[N], m;
int st[19][N];
void sort() {
  for (int i = 1; i <= m; i++) c[i] = 0;
  for (int i = 1; i <= n; i++) c[a[b[i]]]++;
  for (int i = 2; i <= m; i++) c[i] += c[i - 1];
  for (int i = n; i >= 1; i--) sa[c[a[b[i]]]--] = b[i];
}
void calc(int _n, char *str) {
  n = _n;
  m = 0;
  for (int i = 1; i <= n; i++) {
    a[i] = str[i] - 'a' + 1;
    b[i] = i;
    m = max(m, a[i]);
  }
  sort();
  int all = -1;
  for (int l = 1; all < n; l <<= 1) {
    all = 0;
    for (int i = n - l + 1; i <= n; i++) b[++all] = i;
    for (int i = 1; i <= n; i++)
      if (sa[i] > l) b[++all] = sa[i] - l;
    sort();
    swap(a, b);
    a[sa[1]] = all = 1;
    for (int i = 2; i <= n; i++) {
      if (!(b[sa[i]] == b[sa[i - 1]] && b[sa[i] + l] == b[sa[i - 1] + l]))
        all++;
      a[sa[i]] = all;
    }
    m = all;
  }
  for (int i = 1; i <= n; i++) rk[i] = a[i];
  for (int i = 1, j = 0; i <= n; i++) {
    if (j) j--;
    while (str[i + j] == str[sa[rk[i] - 1] + j]) j++;
    h[rk[i]] = j;
  }
  for (int i = 1; i < n; i++) st[0][i] = h[i + 1];
  for (int j = 1; j <= 18; j++)
    for (int i = 1; i < n; i++) {
      st[j][i] = st[j - 1][i];
      if (i + (1 << (j - 1)) < n)
        st[j][i] = min(st[j][i], st[j - 1][i + (1 << (j - 1))]);
    }
}
int getLCP(int a, int b) {
  if (a == b) return -1;
  a = rk[a];
  b = rk[b];
  if (a > b) swap(a, b);
  b--;
  int j = (int)log2(b - a + 1);
  return min(st[j][a], st[j][b - (1 << j) + 1]);
}
bool same(int a, int b, int len) { return getLCP(a, b) >= len; }
bool query(int l, int r) {
  int lcp;
  int x = rk[l];
  lcp = h[x];
  for (int i = x - 1; i >= 1 && x - i + 1 <= sqrtn; lcp = min(lcp, h[i]), i--)
    if ((l < sa[i] && sa[i] <= r) && sa[i] + lcp - 1 >= r) return true;
  lcp = h[x + 1];
  for (int i = x + 1; i <= n && i - x + 1 <= sqrtn; i++, lcp = min(lcp, h[i]))
    if ((l < sa[i] && sa[i] <= r) && sa[i] + lcp - 1 >= r) return true;
  return false;
}
}  // namespace SA
namespace HASH {
const unsigned long long BAS = 133333;
unsigned long long bas[N];
unsigned long long h[N];
void calc(int n, char *str) {
  bas[0] = 1;
  for (int i = 1; i <= n; i++) bas[i] = bas[i - 1] * BAS;
  for (int i = 1; i <= n; i++) h[i] = h[i - 1] + bas[i] * (str[i] - 'a' + 1);
}
bool same(int x, int y, int len) {
  if (x > y) swap(x, y);
  return (h[x + len - 1] - h[x - 1]) * bas[y - x] ==
         (h[y + len - 1] - h[y - 1]);
}
}  // namespace HASH
namespace MATH {
bool vis[N];
int p[N], pcnt;
int minp[N];
void sieve(int n) {
  for (int i = 2; i <= n; i++) {
    if (!vis[i]) {
      p[++pcnt] = i;
      minp[i] = i;
    }
    for (int j = 1; j <= pcnt && i * p[j] <= n; j++) {
      vis[i * p[j]] = true;
      minp[i * p[j]] = p[j];
      if (i % p[j] == 0) break;
    }
  }
}
void getP(int x, int *a, int &acnt) {
  acnt = 0;
  while (x != 1) {
    int curp = minp[x];
    a[++acnt] = curp;
    while (x % curp == 0) x /= curp;
  }
}
}  // namespace MATH
namespace DIV {
struct Seg {
  int rt, sz;
  int ch[N * 2][2];
  int tag[N * 2];
  void reset() { rt = sz = 0; }
  inline void makeTag(int u, int x) { tag[u] = min(tag[u], x); }
  void build(int &u, int l, int r) {
    u = ++sz;
    tag[u] = INF;
    if (l == r) return;
    int mid = (l + r) >> 1;
    build(ch[u][0], l, mid);
    build(ch[u][1], mid + 1, r);
  }
  void segMin(int u, int l, int r, int ml, int mr, int x) {
    if (ml > mr) return;
    if (ml <= l && r <= mr) {
      makeTag(u, x);
      return;
    }
    int mid = (l + r) >> 1;
    if (mr <= mid)
      segMin(ch[u][0], l, mid, ml, mr, x);
    else if (mid < ml)
      segMin(ch[u][1], mid + 1, r, ml, mr, x);
    else {
      segMin(ch[u][0], l, mid, ml, mid, x);
      segMin(ch[u][1], mid + 1, r, mid + 1, mr, x);
    }
  }
  void extract(int u, int l, int r, int *arr, int tagSum = INF) {
    tagSum = min(tagSum, tag[u]);
    if (l == r) {
      arr[l] = tagSum;
      return;
    }
    int mid = (l + r) >> 1;
    extract(ch[u][0], l, mid, arr, tagSum);
    extract(ch[u][1], mid + 1, r, arr, tagSum);
  }
} sl, sr;
int f[2][N];
int st[2][19][N];
void getNext(int n, char *str, int *nex) {
  int i = 0, j, p;
  nex[0] = n;
  while (i < n - 1 && str[i] == str[i + 1]) i++;
  nex[1] = i;
  p = 1;
  for (int i = 2; i < n; i++)
    if (nex[i - p] + i < nex[p] + p)
      nex[i] = nex[i - p];
    else {
      j = max(0, p + nex[p] - i);
      while (i + j < n && str[j] == str[i + j]) j++;
      nex[i] = j;
      p = i;
    }
}
void getExt(int n, char *s, int m, char *t, int *nex, int *ext) {
  int i = 0, j, p;
  while (i < n && i < m && s[i] == t[i]) i++;
  ext[0] = i;
  p = 0;
  for (int i = 1; i < n; i++)
    if (nex[i - p] + i < ext[p] + p)
      ext[i] = nex[i - p];
    else {
      j = max(0, p + ext[p] - i);
      while (i + j < n && j < m && s[i + j] == t[j]) j++;
      ext[i] = j;
      p = i;
    }
}
void proceed(int l, int mid, int r, int *lp, int *ls) {
  int lenl = mid - l + 1, lenr = r - mid;
  static int nex[N];
  getNext(lenr, str + mid + 1, lp + 1);
  lp[lenr + 1] = 0;
  reverse(str + l, str + mid + 1);
  reverse(str + mid + 1, str + r + 1);
  getNext(lenl, str + l, nex);
  getExt(lenr, str + mid + 1, lenl, str + l, nex, ls + 1);
  reverse(ls + 1, ls + 1 + lenr);
  reverse(str + l, str + mid + 1);
  reverse(str + mid + 1, str + r + 1);
}
void divide(int l, int r) {
  if (l == r) return;
  int mid = (l + r) >> 1;
  divide(l, mid);
  divide(mid + 1, r);
  static int lp[N], ls[N];
  proceed(l, mid, r, lp, ls);
  for (int x = 1; x <= r - mid; x++) {
    int tl = mid + max(1, 2 * x - ls[x]),
        tr = mid + min(r - mid, min(2 * x - 1, x + lp[x + 1]));
    sl.segMin(sl.rt, 1, n, tl, tr, 2 * x);
    sr.segMin(sr.rt, 1, n, tl - 2 * x + 1, tr - 2 * x + 1, 2 * x);
  }
  reverse(str + l, str + r + 1);
  mid = l + (r - mid) - 1;
  proceed(l, mid, r, lp, ls);
  for (int x = 1; x <= r - mid; x++) {
    int tl = mid + max(1, 2 * x - ls[x]),
        tr = mid + min(r - mid, min(2 * x - 1, x + lp[x + 1]));
    tl = l + (r - tl);
    tr = l + (r - tr);
    sr.segMin(sr.rt, 1, n, tr, tl, 2 * x);
    sl.segMin(sr.rt, 1, n, tr + 2 * x - 1, tl + 2 * x - 1, 2 * x);
  }
  reverse(str + l, str + r + 1);
}
void initST() {
  for (int i = 1; i <= n; i++) st[0][0][i] = f[0][i];
  for (int j = 1; j <= 18; j++)
    for (int i = 1; i <= n; i++) {
      st[0][j][i] = st[0][j - 1][i];
      if (i + (1 << (j - 1)) <= n)
        st[0][j][i] = max(st[0][j][i], st[0][j - 1][i + (1 << (j - 1))]);
    }
  for (int i = 1; i <= n; i++) st[1][0][i] = f[1][i];
  for (int j = 1; j <= 18; j++)
    for (int i = 1; i <= n; i++) {
      st[1][j][i] = st[1][j - 1][i];
      if (i + (1 << (j - 1)) <= n)
        st[1][j][i] = min(st[1][j][i], st[1][j - 1][i + (1 << (j - 1))]);
    }
}
int query(int k, int l, int r) {
  int j = (int)log2(r - l + 1);
  if (!k)
    return max(st[0][j][l], st[0][j][r - (1 << j) + 1]);
  else
    return min(st[1][j][l], st[1][j][r - (1 << j) + 1]);
}
void calc() {
  sl.build(sl.rt, 1, n);
  sr.build(sr.rt, 1, n);
  divide(1, n);
  sl.extract(sl.rt, 1, n, f[0]);
  sr.extract(sr.rt, 1, n, f[1]);
  for (int i = 1; i <= n; i++) {
    f[0][i] = max(0, i - f[0][i] + 1);
    f[1][i] = min(n + 1, i + f[1][i] - 1);
  }
  initST();
}
}  // namespace DIV
void readData() {
  scanf("%d", &n);
  sqrtn = ((int)sqrt(n)) + 1;
  scanf("%s", str + 1);
}
void initStr() {
  for (int i = 1; i <= n; i++) {
    for (int j = 0; j < C; j++) csum[i][j] = csum[i - 1][j];
    csum[i][str[i] - 'a']++;
  }
}
bool allSingle(int l, int r) {
  if (r - l + 1 > 26) return false;
  bool exist = false;
  for (int j = 0; j < C; j++)
    if (csum[r][j] - csum[l - 1][j] >= 2) {
      exist = true;
      break;
    }
  return !exist;
}
bool loop(int l, int r) {
  int len = r - l + 1;
  static int p[20], pcnt;
  MATH::getP(len, p, pcnt);
  for (int i = 1; i <= pcnt; i++) {
    int orz = len / p[i];
    if (SA::same(l, l + orz, len - orz)) return true;
  }
  return false;
}
bool existBorder(int l, int r) {
  int len = r - l + 1;
  for (int i = 1; i < len && i <= sqrtn; i++)
    if (SA::same(l, r - i + 1, i)) return true;
  return SA::query(l, r);
}
bool sideAdjTwo(int l, int r) { return DIV::f[1][l] <= r || l <= DIV::f[0][r]; }
bool adjTwo(int l, int r) {
  return DIV::query(0, l, r) >= l || DIV::query(1, l, r) <= r;
}
bool twoCha(int l, int r) {
  int lc = str[l] - 'a', rc = str[r] - 'a';
  return csum[r][lc] - csum[l - 1][lc] > 1 || csum[r][rc] - csum[l - 1][rc] > 1;
}
int solve(int l, int r) {
  if (allSingle(l, r)) return -1;
  if (loop(l, r)) return 1;
  if (existBorder(l, r)) return 2;
  if (sideAdjTwo(l, r)) return 2;
  if (adjTwo(l, r)) return 3;
  if (twoCha(l, r)) return 3;
  return 4;
}
void answerQuery() {
  int q, l, r;
  scanf("%d", &q);
  for (int i = 1; i <= q; i++) {
    scanf("%d%d", &l, &r);
    printf("%d\n", solve(l, r));
  }
}
int main() {
  readData();
  initStr();
  SA::calc(n, str);
  HASH::calc(n, str);
  MATH::sieve(n);
  DIV::calc();
  answerQuery();
  return 0;
}