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;
template <typename T>
void maxtt(T& t1, T t2) {
  t1 = max(t1, t2);
}
template <typename T>
void mintt(T& t1, T t2) {
  t1 = min(t1, t2);
}
bool debug = 0;
int n, m, k;
int dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0};
string direc = "RDLU";
long long ln, lk, lm;
void etp(bool f = 0) {
  puts(f ? "YES" : "NO");
  exit(0);
}
void addmod(int& x, int y, int mod = 998244353) {
  assert(y >= 0);
  x += y;
  if (x >= mod) x -= mod;
  assert(x >= 0 && x < mod);
}
void et(int x = -1) {
  printf("%d\n", x);
  exit(0);
}
long long fastPow(long long x, long long y, int mod = 998244353) {
  long long ans = 1;
  while (y > 0) {
    if (y & 1) ans = (x * ans) % mod;
    x = x * x % mod;
    y >>= 1;
  }
  return ans;
}
long long gcd1(long long x, long long y) { return y ? gcd1(y, x % y) : x; }
const int MN = (1 << 20) + 5;
struct mb {
  int M, T[MN];
  void init(int sz) {
    for (M = 1; sz + 2 > M; M <<= 1)
      ;
  }
  void init(int sz, int val) {
    for (M = 1; sz + 2 > M; M <<= 1)
      ;
    for (int(i) = 0; (i) < (int)(MN); (i)++) T[i] = val;
  }
  void clear() { memset(T, 0, sizeof T); }
  void upt(int l, int r, int val) {
    for (l = l + M - 1, r = r + M + 1; l ^ r ^ 1; l >>= 1, r >>= 1) {
      if (~l & 1) T[l ^ 1] = min(val, T[l ^ 1]);
      if (r & 1) T[r ^ 1] = min(val, T[r ^ 1]);
    }
  }
  int qySingleMin(int l) {
    int ans = (1 << 30);
    l += M;
    while (l >= 1) {
      ans = min(ans, T[l]);
      l >>= 1;
    }
    return ans;
  }
} sl, sr;
namespace MainandLorentz {
vector<int> z_function(string const& s) {
  int n = (int)s.size();
  vector<int> z(n);
  for (int i = 1, l = 0, r = 0; i < n; i++) {
    if (i <= r) z[i] = min(r - i + 1, z[i - l]);
    while (i + z[i] < n && s[z[i]] == s[i + z[i]]) z[i]++;
    if (i + z[i] - 1 > r) {
      l = i;
      r = i + z[i] - 1;
    }
  }
  return z;
}
int get_z(vector<int> const& z, int i) {
  if (0 <= i && i < (int)z.size())
    return z[i];
  else
    return 0;
}
void convert_to_repetitions(int shift, bool left, int cntr, int l, int k1,
                            int k2) {
  int L = max(1, l - k2), R = min(l, k1);
  if (R < L) return;
  if (left) mintt(R, l);
  L = shift + (left ? cntr - L : cntr - l - L + 1);
  R = shift + (left ? cntr - R : cntr - l - R + 1);
  if (R < L) swap(L, R);
  L++;
  R++;
  sl.upt(L, R, l + l - 1);
  sr.upt(L + l + l - 1, R + l + l - 1, l + l - 1);
}
void find_repetitions(string s, int shift = 0) {
  int n = (int)s.size();
  if (n == 1) return;
  int nu = n / 2;
  int nv = n - nu;
  string u = s.substr(0, nu);
  string v = s.substr(nu);
  string ru(u.rbegin(), u.rend());
  string rv(v.rbegin(), v.rend());
  find_repetitions(u, shift);
  find_repetitions(v, shift + nu);
  vector<int> z1 = z_function(ru);
  vector<int> z2 = z_function(v + '#' + u);
  vector<int> z3 = z_function(ru + '#' + rv);
  vector<int> z4 = z_function(v);
  for (int cntr = 0; cntr < n; cntr++) {
    int l, k1, k2;
    if (cntr < nu) {
      l = nu - cntr;
      k1 = get_z(z1, nu - cntr);
      k2 = get_z(z2, nv + 1 + cntr);
    } else {
      l = cntr - nu + 1;
      k1 = get_z(z3, nu + 1 + nv - 1 - (cntr - nu));
      k2 = get_z(z4, (cntr - nu) + 1);
    }
    if (k1 + k2 >= l) convert_to_repetitions(shift, cntr < nu, cntr, l, k1, k2);
  }
}
};  // namespace MainandLorentz
int lg[200135];
struct SuffixArray {
  int sa[200135], rk[200135], height[200135];
  int c[200135], x[200135], y[200135];
  void get_SA1(const char* s, int n, int m = 122) {
    for (int(i) = 1; (i) <= (int)(n); (i)++) ++c[x[i] = s[i]];
    for (int i = 2; 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 = 2; i <= m; ++i) c[i] += c[i - 1];
      for (int i = n; i >= 1; --i) {
        sa[c[x[y[i]]]--] = y[i];
        y[i] = 0;
      }
      swap(x, y);
      x[sa[1]] = 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 (num == n) break;
      m = num;
    }
  }
  void get_height(const char* s, int n) {
    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 (j + k <= n && i + k <= n && s[i + k] == s[j + k]) ++k;
      height[rk[i]] = k;
    }
  }
  void ppTest(const char* s, int n) {
    for (int(i) = 1; (i) <= (int)(n); (i)++) {
      printf("%d: %d ", i, sa[i]);
      for (int j = sa[i]; j <= n; j++) printf("%c", s[j]);
      puts("");
    }
  }
  int T[200135][20];
  void init(int n) {
    for (int(i) = 1; (i) <= (int)(n); (i)++) T[i][0] = height[i];
    for (int j = 1; (1 << j) <= n; j++) {
      for (int i = 1; i + (1 << j) - 1 <= n; i++) {
        T[i][j] = min(T[i][j - 1], T[i + (1 << (j - 1))][j - 1]);
      }
    }
    for (int i = 2; i <= n; i++) lg[i] = lg[i >> 1] + 1;
  }
  int qym(int l, int r) {
    if (r < l) swap(l, r);
    int x = lg[r - l + 1];
    int y = min(T[l][x], T[r - (1 << x) + 1][x]);
    return y;
  }
  int lcp(int x, int y) {
    if (rk[x] > rk[y]) swap(x, y);
    return qym(rk[x] + 1, rk[y]);
  }
  int sq;
  int border(int l, int r) {
    int i, s = (1 << 30);
    for (i = 1; i <= sq && i < r - l + 1; i++) {
      if (lcp(l, r - i + 1) >= i) return i;
    }
    for (i = max(1, rk[l] - sq + 1); i <= min(n, rk[l] + sq - 1); i++) {
      if (l < sa[i] && sa[i] <= r && lcp(l, sa[i]) >= r - sa[i] + 1)
        s = min(s, r - sa[i] + 1);
    }
    return s == (1 << 30) ? (1 << 30) / 2 : s;
  }
} sa;
string S;
char s[200135];
vector<int> pms[200135];
int lt[200135], rt[200135];
struct stb {
  int T[200135][20];
  void init(int n) {
    for (int(i) = 1; (i) <= (int)(n); (i)++) T[i][0] = lt[i];
    for (int j = 1; (1 << j) <= n; j++) {
      for (int i = 1; i + (1 << j) - 1 <= n; i++) {
        T[i][j] = min(T[i][j - 1], T[i + (1 << (j - 1))][j - 1]);
      }
    }
  }
  int qym(int l, int r) {
    if (r < l) swap(l, r);
    int x = lg[r - l + 1];
    int y = min(T[l][x], T[r - (1 << x) + 1][x]);
    return y;
  }
} stQuery;
bool ck1(int l, int r) {
  int len = r - l + 1;
  for (int t : pms[len]) {
    int ct = len / t;
    if (sa.lcp(l, l + ct) >= len - ct) return 1;
  }
  return 0;
}
bool ck2(int l, int r) {
  return lt[l] <= r || rt[r] >= l || sa.border(l, r) * 2 <= r - l + 1;
}
int psum[200135][26];
int get(int l, int r, int c) { return psum[r][c - 'a'] - psum[l - 1][c - 'a']; }
bool ck3(int l, int r) {
  return get(l + 1, r, s[l]) || get(l, r - 1, s[r]) || stQuery.qym(l, r) <= r;
}
void fmain(int tid) {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> n >> S;
  sl.init(n, (1 << 30));
  sr.init(n, (1 << 30));
  MainandLorentz::find_repetitions(S);
  for (int(i) = 1; (i) <= (int)(n); (i)++) {
    int z = sl.qySingleMin(i);
    if (z < (1 << 30))
      lt[i] = i + z;
    else
      lt[i] = n + 10;
    z = sr.qySingleMin(i);
    if (z < (1 << 30)) {
      rt[i] = i - z;
      assert(z < i);
    }
  }
  cin >> m;
  for (int i = 2; i <= n; i++) {
    if (pms[i].empty()) {
      for (int j = i; j <= n; j += i) pms[j].push_back(i);
    }
  }
  for (int(i) = 1; (i) <= (int)(n); (i)++) s[i] = S[i - 1];
  sa.get_SA1(s, n);
  sa.get_height(s, n);
  sa.init(n);
  sa.sq = sqrt(n);
  for (int(i) = 1; (i) <= (int)(n); (i)++) {
    for (int(j) = 0; (j) < (int)(26); (j)++) psum[i][j] += psum[i - 1][j];
    psum[i][s[i] - 'a']++;
  }
  stQuery.init(n);
  for (int(i) = 1; (i) <= (int)(m); (i)++) {
    int l, r;
    cin >> l >> r;
    if (r - l + 1 <= 26) {
      vector<int> vp(30, 0);
      bool ok = 0;
      for (int j = l; j <= r; j++) {
        vp[s[j] - 'a']++;
        if (vp[s[j] - 'a'] == 2) {
          ok = 1;
          break;
        }
      }
      if (!ok) {
        puts("-1");
        continue;
      }
    }
    if (ck1(l, r))
      puts("1");
    else if (ck2(l, r))
      puts("2");
    else if (ck3(l, r))
      puts("3");
    else
      puts("4");
  }
}
int main() {
  int t = 1;
  for (int(i) = 1; (i) <= (int)(t); (i)++) {
    fmain(i);
  }
  return 0;
}