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>
const int INF = 1e9 + 1;
const double EPS = 1e-10;
const long long P = 2e9 + 9;
const long long Q = 47;
using namespace std;
void hello() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
}
void show(vector<int>& arr) {
  for (auto i : arr) cout << i << " ";
  cout << "\n";
}
void show(vector<vector<int>>& arr) {
  for (auto i : arr) show(i);
  cout << "\n";
}
vector<int> st;
vector<int> sarr;
vector<int> order;
void build_sarr(string& s) {
  int n = s.size() + 1;
  vector<int> cls(n);
  for (int i = 0; i < n - 1; ++i) cls[i] = s[i] - 'a';
  cls.back() = -1;
  vector<pair<pair<int, int>, int>> tmp(n);
  for (int i = 1; i < n; i *= 2) {
    for (int j = 0; j < n; ++j) tmp[j] = {{cls[j], cls[(j + i) % n]}, j};
    stable_sort(tmp.begin(), tmp.end());
    int last = 0;
    for (int j = 0; j < n; ++j) {
      if (j && tmp[j].first != tmp[j - 1].first) ++last;
      cls[tmp[j].second] = last;
    }
  }
  for (int i = 0; i < n - 1; ++i) sarr[cls[i] - 1] = i;
}
const int MAXN = 2e5;
const int MAXLOG = 20;
struct minx {
  vector<vector<int>> tree;
  minx() {}
  minx(vector<int>& data) {
    tree.resize(MAXLOG);
    for (auto& i : tree) i.resize(MAXN);
    for (int i = 0; i < data.size(); ++i) tree[0][i] = data[i];
    for (int i = 1; i < MAXLOG; ++i)
      for (int j = 0; j < data.size(); ++j) {
        if (j + (1 << i) - 1 >= data.size()) break;
        tree[i][j] = min(tree[i - 1][j], tree[i - 1][j + (1 << (i - 1))]);
      }
  }
  int query(int l, int r) {
    int len = r - l + 1;
    return min(tree[st[len]][l], tree[st[len]][r - (1 << st[len]) + 1]);
  }
};
int nlcp;
minx lcp_tree;
int query_lcp(int l, int r) {
  if (l > r) return 0;
  if (order[r] < order[l]) swap(l, r);
  return lcp_tree.query(min(order[l], order[r] - 1),
                        max(order[l], order[r] - 1));
}
void build_pfunc(string& s, vector<int>& pf) {
  for (int i = 1; i < s.size(); ++i) {
    int x = pf[i - 1];
    while (x && s[i] != s[x]) x = pf[x - 1];
    if (s[x] == s[i]) ++x;
    pf[i] = x;
  }
}
void build_zfunc(string& s, vector<int>& zf) {
  int l = -1, r = -1;
  for (int i = 1; i < s.size(); ++i) {
    if (i > r) l = r = i;
    zf[i] = min(zf[i - l], r - i + 1);
    while (i + zf[i] < s.size() && s[zf[i]] == s[i + zf[i]]) ++zf[i];
    if (i + zf[i] - 1 > r) {
      l = i, r = i + zf[i] - 1;
    }
  }
}
void build_tandem(int l, int r, string& s, vector<int>& tandem,
                  vector<int>& rtandem) {
  if (l > r) return;
  if (l == r) {
    rtandem[l] = tandem[l] = s.size();
    return;
  }
  int m = (l + r) / 2;
  build_tandem(l, m, s, tandem, rtandem);
  build_tandem(m + 1, r, s, tandem, rtandem);
  string ts;
  for (int i = m; i >= l; --i) ts += s[i];
  vector<int> pf(m - l + 1);
  vector<int> zf(r - l + 1);
  build_zfunc(ts, pf);
  reverse(pf.begin(), pf.end());
  ts.clear();
  for (int i = m; i >= l; --i) ts += s[i];
  for (int i = r; i > m; --i) ts += s[i];
  build_zfunc(ts, zf);
  reverse(zf.begin(), zf.end());
  vector<vector<int>> order;
  for (int i = l + 1; i <= m; ++i) {
    int v1 = min(query_lcp(i, m + 1), m - i + 1);
    int v2 = pf[i - l - 1];
    order.push_back({i - v2, m - i + 1, i - (m - i + 1) + v1});
  }
  for (int i = m + 2; i < r; ++i) {
    int v1 = zf[i - m - 1];
    int v2 = min(query_lcp(m + 1, i + 1), i - m);
    order.push_back({m - v1 + 1, i - m, m - (i - m) + v2 + 1});
  }
  stable_sort(order.begin(), order.end());
  set<pair<int, int>> mres;
  int pt = 0;
  for (int i = l; i <= r; ++i) {
    while (pt != order.size() && order[pt][0] <= i) {
      mres.insert({order[pt][1], order[pt][2]});
      ++pt;
    }
    while (mres.size() && mres.begin()->second < i) mres.erase(mres.begin());
    if (mres.size()) {
      tandem[i] = min(tandem[i], mres.begin()->first);
    }
  }
  for (auto& val : order) {
    val[0] = val[0] + 2 * val[1] - 1;
    val[2] = val[2] + 2 * val[1] - 1;
  }
  stable_sort(order.begin(), order.end());
  mres.clear();
  pt = 0;
  for (int i = l; i <= r; ++i) {
    while (pt != order.size() && order[pt][0] <= i) {
      mres.insert({order[pt][1], order[pt][2]});
      ++pt;
    }
    while (mres.size() && mres.begin()->second < i) mres.erase(mres.begin());
    if (mres.size()) rtandem[i] = min(rtandem[i], mres.begin()->first);
  }
  for (int i = l; i <= m; ++i) {
    if (query_lcp(i, m + 1) >= m - i + 1) {
      tandem[i] = min(tandem[i], m - i + 1);
      rtandem[i + 2 * (m - i + 1) - 1] =
          min(rtandem[i + 2 * (m - i + 1) - 1], m - i + 1);
    }
  }
}
void build_rtandem(string& s, vector<int>& tandem) {
  fill(tandem.begin(), tandem.end(), s.size());
  for (int i = 0; i < s.size(); ++i) {
    for (int j = i + 1; j < s.size(); ++j)
      if (query_lcp(i, j) >= j - i) {
        tandem[j + j - i - 1] = min(tandem[j + j - i - 1], j - i);
      }
  }
}
signed main() {
  hello();
  int n;
  string s;
  cin >> n >> s;
  sarr.resize(n);
  st.resize(n + 1);
  int cst = -1;
  for (int i = 1; i < n + 1; ++i) {
    if ((1 << (cst + 1)) == i) ++cst;
    st[i] = cst;
  }
  build_sarr(s);
  vector<int> lcp(n, -1);
  order.resize(n);
  int last = 0;
  for (int i = 0; i < n; ++i) order[sarr[i]] = i;
  for (int i = 0; i < n; ++i) {
    if (last) --last;
    if (order[i] == n - 1) continue;
    int v = i, u = sarr[order[i] + 1];
    while (max(v, u) + last < n && s[v + last] == s[u + last]) ++last;
    lcp[order[i]] = last;
  }
  lcp.pop_back();
  nlcp = lcp.size();
  lcp_tree = minx(lcp);
  vector<int> tandem(n), rtandem(n);
  build_tandem(0, n - 1, s, tandem, rtandem);
  vector<int> left(n), right(n);
  int sz = 500;
  vector<int> lst(26, -1);
  for (int i = 0; i < n; ++i) {
    right[i] = lst[s[i] - 'a'];
    lst[s[i] - 'a'] = i;
  }
  fill(lst.begin(), lst.end(), n);
  for (int i = n - 1; i >= 0; --i) {
    left[i] = lst[s[i] - 'a'];
    lst[s[i] - 'a'] = i;
  }
  minx left_tree(left);
  vector<int> tmp(n);
  for (int i = 0; i < n; ++i) tmp[i] = i + tandem[i] * 2 - 1;
  minx tandem_tree(tmp);
  int q;
  cin >> q;
  for (int v = 0; v < q; ++v) {
    int l, r;
    cin >> l >> r;
    --l;
    --r;
    bool flag = 0;
    if (left_tree.query(l, r) > r) {
      cout << -1 << "\n";
      continue;
    }
    int len = r - l + 1;
    for (int val = 1; val * val <= len; ++val) {
      if (len % val) continue;
      if (query_lcp(l, l + val) >= len - val) {
        flag = 1;
      }
      if (val == 1) continue;
      if (query_lcp(l, l + len / val) >= len - len / val) {
        flag = 1;
      }
    }
    if (flag) {
      cout << 1 << "\n";
      continue;
    }
    if (l + 2 * tandem[l] - 1 <= r) {
      cout << 2 << "\n";
      continue;
    }
    if (r - 2 * rtandem[r] + 1 >= l) {
      cout << 2 << "\n";
      continue;
    }
    int best = INF;
    for (int pos = 1; pos < sz; ++pos) {
      if (r - pos + 1 == l) break;
      if (query_lcp(l, r - pos + 1) >= pos) {
        best = min(best, pos);
      }
    }
    int minn = INF;
    for (int pos = order[l]; pos < order[l] + sz; ++pos) {
      if (pos + 1 >= n) break;
      minn = min(minn, lcp[pos]);
      if (sarr[pos + 1] + minn - 1 >= r && sarr[pos + 1] <= r &&
          sarr[pos + 1] >= l) {
        best = min(best, r - sarr[pos + 1] + 1);
      }
    }
    minn = INF;
    for (int pos = order[l] - 1; pos >= order[l] - sz; --pos) {
      if (pos < 0) break;
      minn = min(minn, lcp[pos]);
      if (sarr[pos] + minn - 1 >= r && sarr[pos] <= r && sarr[pos] >= l) {
        best = min(best, r - sarr[pos] + 1);
      }
    }
    if (2 * best <= (r - l + 1)) {
      cout << 2 << "\n";
      continue;
    }
    if (left[l] <= r) {
      cout << 3 << "\n";
      continue;
    }
    if (right[r] >= l) {
      cout << 3 << "\n";
      continue;
    }
    if (tandem_tree.query(l, r) <= r) {
      cout << 3 << "\n";
      continue;
    }
    cout << 4 << "\n";
  }
}