Problem: John Doe has a crooked fence, consisting of n rectangular planks, lined up from the left to the right: the plank that goes i-th (1 ≤ i ≤ n) (from left to right) has width 1 and height hi. We will assume that the plank that goes i-th (1 ≤ i ≤ n) (from left to right) has index i.

A piece of the fence from l to r (1 ≤ l ≤ r ≤ n) is a sequence of planks of wood with indices from l to r inclusive, that is, planks with indices l, l + 1, ..., r. The width of the piece of the fence from l to r is value r - l + 1.

Two pieces of the fence from l1 to r1 and from l2 to r2 are called matching, if the following conditions hold:

  * the pieces do not intersect, that is, there isn't a single plank, such that it occurs in both pieces of the fence; 
  * the pieces are of the same width; 
  * for all i (0 ≤ i ≤ r1 - l1) the following condition holds: hl1 + i + hl2 + i = hl1 + hl2. 



John chose a few pieces of the fence and now wants to know how many distinct matching pieces are for each of them. Two pieces of the fence are distinct if there is a plank, which belongs to one of them and does not belong to the other one.

Input

The first line contains integer n (1 ≤ n ≤ 105) — the number of wood planks in the fence. The second line contains n space-separated integers h1, h2, ..., hn (1 ≤ hi ≤ 109) — the heights of fence planks.

The third line contains integer q (1 ≤ q ≤ 105) — the number of queries. Next q lines contain two space-separated integers li and ri (1 ≤ li ≤ ri ≤ n) — the boundaries of the i-th piece of the fence.

Output

For each query on a single line print a single integer — the number of pieces of the fence that match the given one. Print the answers to the queries in the order, in which the queries are given in the input.

Examples

Input

10
1 2 2 1 100 99 99 100 100 100
6
1 4
1 2
3 4
1 5
9 10
10 10


Output

1
2
2
0
2
9

Solution (in CPP):

Solution:
#include <bits/stdc++.h>
using namespace std;
const int N = 2e5;
pair<int, int> sf[N];
bool sacomp(int lhs, int rhs) { return sf[lhs] < sf[rhs]; }
vector<int> constructSA(vector<int>& s) {
  int n = s.size();
  vector<int> sa(n), r(n);
  for (int i = 0; i < n; i++) r[i] = s[i];
  for (int m = 1; m < n; m *= 2) {
    for (int i = 0; i < n; i++)
      sa[i] = i, sf[i] = {r[i], i + m < n ? r[i + m] : -1};
    stable_sort(sa.begin(), sa.end(), sacomp);
    r[sa[0]] = 0;
    for (int i = 1; i < n; i++)
      r[sa[i]] = sf[sa[i]] != sf[sa[i - 1]] ? i : r[sa[i - 1]];
  }
  return sa;
}
vector<int> computeLCP(vector<int>& s, vector<int>& sa) {
  int n = s.size(), L = 0;
  vector<int> lcp(n), plcp(n), phi(n);
  phi[sa[0]] = -1;
  for (int i = 1; i < n; i++) phi[sa[i]] = sa[i - 1];
  for (int i = 0; i < n; i++) {
    if (phi[i] < 0) {
      plcp[i] = 0;
      continue;
    }
    while (s[i + L] == s[phi[i] + L]) L++;
    plcp[i] = L;
    L = max(L - 1, 0);
  }
  for (int i = 0; i < n; i++) lcp[i] = plcp[sa[i]];
  return lcp;
}
struct STree {
  vector<int> t;
  int n;
  STree(int n) : n(n), t(2 * n) {}
  void upd(int p, int v) {
    for (t[p += n] = v; p > 1; p >>= 1) t[p >> 1] = min(t[p], t[p ^ 1]);
  }
  int query(int l, int r) {
    int res = 1e9;
    for (l += n, r += n; l < r; l >>= 1, r >>= 1) {
      if (l & 1) res = min(res, t[l++]);
      if (r & 1) res = min(res, t[--r]);
    }
    return res;
  }
};
struct STree2 {
  vector<int> st, L, R;
  int n, sz, rt;
  STree2(int n) : st(1, 0), L(1, 0), R(1, 0), n(n), rt(0), sz(1) {}
  int new_node(int v, int l = 0, int r = 0) {
    int ks = ((int)st.size());
    st.push_back(v);
    L.push_back(l);
    R.push_back(r);
    return ks;
  }
  int upd(int k, int s, int e, int p, int v) {
    int ks = new_node(st[k], L[k], R[k]);
    if (s + 1 == e) {
      st[ks] += v;
      return ks;
    }
    int m = (s + e) / 2, ps;
    if (p < m)
      ps = upd(L[ks], s, m, p, v), L[ks] = ps;
    else
      ps = upd(R[ks], m, e, p, v), R[ks] = ps;
    st[ks] = (st[L[ks]] + st[R[ks]]);
    return ks;
  }
  int query(int k, int s, int e, int a, int b) {
    if (e <= a || b <= s) return 0;
    if (a <= s && e <= b) return st[k];
    int m = (s + e) / 2;
    return (query(L[k], s, m, a, b) + query(R[k], m, e, a, b));
  }
  int upd(int k, int p, int v) { return rt = upd(k, 0, n, p, v); }
  int upd(int p, int v) { return upd(rt, p, v); }
  int query(int k, int a, int b) { return query(k, 0, n, a, b); };
};
struct Query {
  int l, r, id, bl, br, ans;
};
int main() {
  int n;
  cin >> n;
  int h[n], d[n - 1], di[n - 1];
  for (int i = 0; i < n; i++) cin >> h[i];
  vector<int> v;
  for (int i = 0; i < n - 1; i++) {
    d[i] = h[i] - h[i + 1];
    di[i] = -d[i];
    v.push_back(d[i]);
  }
  v.push_back(1e9 + 1);
  for (int i = 0; i < n - 1; i++) v.push_back(di[i]);
  v.push_back(1e9 + 2);
  auto sa = constructSA(v);
  auto lcp = computeLCP(v, sa);
  vector<int> sai(((int)sa.size()));
  for (int i = 0; i < ((int)sa.size()); i++) sai[sa[i]] = i;
  STree rmq(((int)lcp.size()));
  for (int i = 0; i < ((int)lcp.size()); i++) rmq.upd(i, lcp[i]);
  vector<int> roots = {0};
  STree2 rmq2(((int)sa.size()));
  for (int i = 0; i < ((int)sa.size()); i++)
    roots.push_back(rmq2.upd(sa[i], 1));
  int q;
  cin >> q;
  vector<Query> queries;
  for (int i = 0; i < q; i++) {
    int l, r;
    cin >> l >> r;
    queries.push_back({l - 1, r - 1, i, -1, -1, -1});
  }
  for (auto& query : queries) {
    int where = sai[query.l];
    int dist = query.r - query.l;
    query.bl = query.br = where;
    int izq = 0, der = where - 1;
    while (izq <= der) {
      int med = (izq + der) / 2;
      if (rmq.query(med + 1, where + 1) >= dist)
        der = med - 1, query.bl = med;
      else
        izq = med + 1;
    }
    izq = where + 1, der = ((int)lcp.size()) - 1;
    while (izq <= der) {
      int med = (izq + der) / 2;
      if (rmq.query(where + 1, med + 1) >= dist)
        izq = med + 1, query.br = med + 1;
      else
        der = med - 1;
    }
    query.ans = rmq2.query(roots[query.br], n, n + query.l - dist) -
                rmq2.query(roots[query.bl], n, n + query.l - dist);
    query.ans +=
        rmq2.query(roots[query.br], n + query.r + 1, ((int)sa.size())) -
        rmq2.query(roots[query.bl], n + query.r + 1, ((int)sa.size()));
    cout << query.ans << endl;
  }
  return 0;
}