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;
int n;
char s[200005];
namespace LCP {
int ra[200005], sa[200005], sum[200005], tem[200005], h[200005];
int v[800005];
void build(int u, int l, int r) {
  if (l == r) {
    v[u] = h[l];
    return;
  }
  int mid = (l + r) >> 1;
  build(u << 1, l, mid);
  build((u << 1) | 1, mid + 1, r);
  v[u] = min(v[u << 1], v[(u << 1) | 1]);
}
void init() {
  for (int i = 1; i <= n; i++) sum[ra[i] = s[i] - 'a' + 1]++;
  for (int i = 1; i <= 26; i++) sum[i] += sum[i - 1];
  for (int i = n; i >= 1; i--) sa[sum[ra[i]]--] = i;
  for (int i = 1, tot = 26; i <= n; i <<= 1) {
    int cnt = 0;
    for (int j = n - i + 1; j <= n; j++) tem[++cnt] = j;
    for (int j = 1; j <= n; j++)
      if (sa[j] > i) tem[++cnt] = sa[j] - i;
    for (int j = 1; j <= n; j++) sum[j] = 0;
    for (int j = 1; j <= n; j++) sum[ra[tem[j]]]++;
    for (int j = 1; j <= n; j++) sum[j] += sum[j - 1];
    for (int j = n; j >= 1; j--) sa[sum[ra[tem[j]]]--] = tem[j];
    tem[sa[1]] = tot = 1;
    for (int j = 2; j <= n; j++)
      if (ra[sa[j]] == ra[sa[j - 1]] &&
          (sa[j] + i <= n ? ra[sa[j] + i] : -1) ==
              (sa[j - 1] + i <= n ? ra[sa[j - 1] + i] : -1))
        tem[sa[j]] = tot;
      else
        tem[sa[j]] = ++tot;
    swap(tem, ra);
    if (tot == n) break;
  }
  for (int i = 1; i <= n; i++) {
    h[ra[i]] = max(h[ra[i - 1]] - 1, 0);
    for (int &j = h[ra[i]]; s[i + j] == s[sa[ra[i] - 1] + j]; j++)
      ;
  }
  build(1, 1, n);
}
inline int query(int u, int l, int r, int L, int R) {
  if (L <= l && r <= R) {
    return v[u];
  }
  int mid = (l + r) >> 1;
  if (R <= mid) return query(u << 1, l, mid, L, R);
  if (L > mid) return query((u << 1) | 1, mid + 1, r, L, R);
  return min(query(u << 1, l, mid, L, R),
             query((u << 1) | 1, mid + 1, r, L, R));
}
int lcp(int a, int b) {
  int x = ra[a], y = ra[b];
  if (x > y) swap(x, y);
  return query(1, 1, n, x + 1, y);
}
}  // namespace LCP
using LCP::lcp;
namespace rLCP {
char s[200005];
int ra[200005], sa[200005], sum[200005], tem[200005], h[200005];
int v[800005];
void build(int u, int l, int r) {
  if (l == r) {
    v[u] = h[l];
    return;
  }
  int mid = (l + r) / 2;
  build(u << 1, l, mid);
  build((u << 1) | 1, mid + 1, r);
  v[u] = min(v[u << 1], v[(u << 1) | 1]);
}
void init() {
  for (int i = 1; i <= n; i++) sum[ra[i] = s[i] - 'a' + 1]++;
  for (int i = 1; i <= 26; i++) sum[i] += sum[i - 1];
  for (int i = n; i >= 1; i--) sa[sum[ra[i]]--] = i;
  for (int i = 1, tot = 26; i <= n; i <<= 1) {
    int cnt = 0;
    for (int j = n - i + 1; j <= n; j++) tem[++cnt] = j;
    for (int j = 1; j <= n; j++)
      if (sa[j] > i) tem[++cnt] = sa[j] - i;
    for (int j = 1; j <= n; j++) sum[j] = 0;
    for (int j = 1; j <= n; j++) sum[ra[tem[j]]]++;
    for (int j = 1; j <= n; j++) sum[j] += sum[j - 1];
    for (int j = n; j >= 1; j--) sa[sum[ra[tem[j]]]--] = tem[j];
    tem[sa[1]] = tot = 1;
    for (int j = 2; j <= n; j++)
      if (ra[sa[j]] == ra[sa[j - 1]] &&
          (sa[j] + i <= n ? ra[sa[j] + i] : -1) ==
              (sa[j - 1] + i <= n ? ra[sa[j - 1] + i] : -1))
        tem[sa[j]] = tot;
      else
        tem[sa[j]] = ++tot;
    swap(tem, ra);
    if (tot == n) break;
  }
  for (int i = 1; i <= n; i++) {
    h[ra[i]] = max(h[ra[i - 1]] - 1, 0);
    for (int &j = h[ra[i]]; s[i + j] == s[sa[ra[i] - 1] + j]; j++)
      ;
  }
  build(1, 1, n);
}
inline int query(int u, int l, int r, int L, int R) {
  if (L <= l && r <= R) {
    return v[u];
  }
  int mid = (l + r) / 2;
  if (R <= mid) return query(u << 1, l, mid, L, R);
  if (L > mid) return query((u << 1) | 1, mid + 1, r, L, R);
  return min(query(u << 1, l, mid, L, R),
             query((u << 1) | 1, mid + 1, r, L, R));
}
int rlcp(int a, int b) {
  int x = ra[n - a + 1], y = ra[n - b + 1];
  if (x > y) swap(x, y);
  return query(1, 1, n, x + 1, y);
}
}  // namespace rLCP
using LCP::lcp;
using rLCP::rlcp;
namespace case0 {
int h[26];
bool check(int l, int r) {
  if (r - l + 1 <= 26) {
    memset(h, 0, sizeof(h));
    bool flag = 0;
    for (int i = l; i <= r; i++) h[s[i] - 'a']++;
    for (int i = 0; i < 26; i++)
      if (h[i] > 1) flag = 1;
    return !flag;
  }
  return false;
}
}  // namespace case0
namespace case1 {
bool check(int l, int r) {
  int len = r - l + 1, L = len;
  for (int k = 2; k * k <= len; k++)
    if (len % k == 0) {
      while (len % k == 0) len /= k;
      if (l + L / k + lcp(l, l + L / k) - 1 >= r) return true;
    }
  if (len > 1)
    if (l + L / len + lcp(l, l + L / len) - 1 >= r) return true;
  return false;
}
}  // namespace case1
namespace case_aa {
int vl[800005], vr[800005];
void modify0(int u, int l, int r, int L, int R, int k) {
  if (L <= l && r <= R) {
    vl[u] = k;
    return;
  }
  int mid = (l + r) / 2;
  if (L <= mid) modify0(u << 1, l, mid, L, R, k);
  if (R > mid) modify0((u << 1) | 1, mid + 1, r, L, R, k);
}
void modify1(int u, int l, int r, int L, int R, int k) {
  if (L <= l && r <= R) {
    vr[u] = k;
    return;
  }
  int mid = (l + r) / 2;
  if (L <= mid) modify1(u << 1, l, mid, L, R, k);
  if (R > mid) modify1((u << 1) | 1, mid + 1, r, L, R, k);
}
int fl[200005], fr[200005];
void maintain(int u, int l, int r) {
  if (l == r) {
    fl[l] = vl[u], fr[l] = vr[u];
    return;
  }
  int mid = (l + r) / 2;
  vl[u << 1] = min(vl[u << 1], vl[u]);
  vl[(u << 1) | 1] = min(vl[(u << 1) | 1], vl[u]);
  vr[u << 1] = min(vr[u << 1], vr[u]);
  vr[(u << 1) | 1] = min(vr[(u << 1) | 1], vr[u]);
  maintain(u << 1, l, mid);
  maintain((u << 1) | 1, mid + 1, r);
}
int w[800005];
void build(int u, int l, int r) {
  if (l == r) {
    w[u] = fl[l];
    return;
  }
  int mid = (l + r) / 2;
  build(u << 1, l, mid);
  build((u << 1) | 1, mid + 1, r);
  w[u] = min(w[u << 1], w[(u << 1) | 1]);
}
void init() {
  memset(vl, 63, sizeof(vl));
  memset(vr, 63, sizeof(vr));
  for (int i = n; i >= 1; i--) {
    for (int j = i; j <= n - i; j += i) {
      int x = lcp(j, j + i), y = rlcp(j - 1, j + i - 1);
      if (x + y >= i) {
        modify0(1, 1, n, j - y, j - i + x, i);
        modify1(1, 1, n, j + i + i - y - 1, j + i + x - 1, i);
      }
    }
  }
  maintain(1, 1, n);
  for (int i = 1; i <= n; i++)
    fl[i] = i + 2 * fl[i] - 1, fr[i] = i - 2 * fr[i] + 1;
  build(1, 1, n);
}
int query(int u, int l, int r, int L, int R) {
  if (L <= l && r <= R) return w[u];
  int mid = (l + r) / 2;
  if (R <= mid) return query(u << 1, l, mid, L, R);
  if (L > mid) return query((u << 1) | 1, mid + 1, r, L, R);
  return min(query(u << 1, l, mid, L, R),
             query((u << 1) | 1, mid + 1, r, L, R));
}
int Q(int l, int r) { return query(1, 1, n, l, r); }
}  // namespace case_aa
namespace case2 {
int B = 100, B2 = 2000;
bool check(int l, int r) {
  if (case_aa::fl[l] <= r || case_aa::fr[r] >= l) return true;
  for (int i = 1; i <= B; i++) {
    if (r - i + 1 > l && lcp(l, r - i + 1) >= i) return true;
  }
  for (int i = 1, Lcp = r - l + 1; i <= B2 && LCP::ra[l] + i <= n; i++) {
    Lcp = min(Lcp, LCP::h[LCP::ra[l] + i]);
    int y = LCP::sa[LCP::ra[l] + i];
    if (y > l && y <= r) {
      if (y + Lcp - 1 >= r) return true;
    }
  }
  for (int i = 1, Lcp = r - l + 1; i <= B2 && LCP::ra[l] - i > 0; i++) {
    Lcp = min(Lcp, LCP::h[LCP::ra[l] - i + 1]);
    int y = LCP::sa[LCP::ra[l] - i];
    if (y > l && y <= r) {
      if (y + Lcp - 1 >= r) return true;
    }
  }
  return false;
}
}  // namespace case2
namespace case3 {
int c[200005][26], p[200005];
void init() {
  for (int i = 1; i <= n; i++) {
    for (int j = 0; j < 26; j++) c[i][j] = c[i - 1][j];
    c[i][s[i] - 'a']++;
  }
}
bool check(int l, int r) {
  if ((c[r][s[l] - 'a'] - c[l][s[l] - 'a'] > 0) ||
      (c[r - 1][s[r] - 'a'] - c[l - 1][s[r] - 'a'] > 0))
    return true;
  if (case_aa::Q(l, r) <= r) return true;
  return false;
}
}  // namespace case3
inline int read() {
  int a = 0;
  char c = getchar();
  while (c < '0' || c > '9') c = getchar();
  while (c >= '0' && c <= '9') a = a * 10 + c - '0', c = getchar();
  return a;
}
int main() {
  n = read();
  scanf("%s", s + 1);
  for (int i = n; i >= 1; i--) rLCP::s[i] = s[n - i + 1];
  LCP::init();
  rLCP::init();
  case_aa::init();
  case3::init();
  int q = read();
  for (int i = 1; i <= q; i++) {
    int l = read(), r = read();
    if (case0::check(l, r)) {
      puts("-1");
      continue;
    }
    if (case1::check(l, r)) {
      puts("1");
      continue;
    }
    if (case2::check(l, r)) {
      puts("2");
      continue;
    }
    if (case3::check(l, r)) {
      puts("3");
      continue;
    }
    puts("4");
    continue;
  }
  return 0;
}