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 MAXN = 200010;
int _count[MAXN];
void countSort(int* in, int* out, const int* key, int N, int M) {
  fill_n(_count, M, 0);
  for (int i = 0; i < (N); ++i) ++_count[key[in[i]]];
  for (int i = (1); i < (M); ++i) _count[i] += _count[i - 1];
  for (int i = ((int)(N)) - 1; i >= (0); --i) out[--_count[key[in[i]]]] = in[i];
}
int temp[MAXN], s0[MAXN], s12[MAXN], _rank[MAXN], recOut[MAXN];
const int* _s;
inline bool cmp(int u, int v) {
  while (true) {
    if (_s[u] != _s[v]) return _s[u] < _s[v];
    if ((u % 3) && (v % 3)) return _rank[u] < _rank[v];
    ++u;
    ++v;
  }
}
void suffixArray(const int* s, int N, int* out, int K) {
  int n0 = (N + 2) / 3, n1 = (N + 1) / 3, n12 = 0;
  for (int i = 0; i < (N); ++i)
    if (i % 3) temp[n12++] = i;
  countSort(temp, s12, s + 2, n12, K);
  countSort(s12, temp, s + 1, n12, K);
  countSort(temp, s12, s, n12, K);
  int recIn[n12 + 5], cnt = 2;
  for (int i = 0; i < (n12); ++i) {
    if (i > 0 && !equal(s + s12[i - 1], s + s12[i - 1] + 3, s + s12[i])) ++cnt;
    recIn[s12[i] % 3 == 1 ? s12[i] / 3 : s12[i] / 3 + n1 + 1] = cnt;
  }
  if (cnt != n12 + 1) {
    for (int i = 0; i < (3); ++i) recIn[n12 + 1 + i] = 0;
    recIn[n1] = 1;
    suffixArray(recIn, n12 + 1, recOut, cnt + 1);
    for (int i = (1); i < (n12 + 1); ++i)
      s12[i - 1] =
          recOut[i] < n1 ? 3 * recOut[i] + 1 : 3 * (recOut[i] - n1) - 1;
  }
  for (int i = 0; i < (n12); ++i) _rank[s12[i]] = i + 1;
  _rank[N] = 0;
  for (int i = 0; i < (n0); ++i) s0[i] = 3 * i;
  countSort(s0, temp, _rank + 1, n0, n12 + 2);
  countSort(temp, s0, s, n0, K);
  _s = s;
  merge(s12, s12 + n12, s0, s0 + n0, out, cmp);
}
void computeLCP(const int* sA, const int* invSA, int N, int* text, int* lcp) {
  int cur = 0;
  for (int i = 0; i < (N); ++i) {
    int j = invSA[i];
    if (!j) continue;
    int k = sA[j - 1];
    while (text[k + cur] == text[i + cur]) cur++;
    lcp[j] = cur;
    cur = max(0, cur - 1);
  }
}
int fence[MAXN], n, K, q;
int suf[MAXN], rsuf[MAXN], lcp[MAXN];
int sufcnt[MAXN];
struct query {
  int l, r, sufl, sufr, i, res;
  int len() const { return r - l + 1; }
};
query Q[100000];
void input() {
  scanf("%d", &n);
  int prev, cur;
  K = 1;
  map<int, int> amap;
  scanf("%d", &prev);
  for (int i = 0; i < (n - 1); ++i) {
    scanf("%d", &cur);
    int x = prev - cur;
    prev = cur;
    if (amap.find(x) == amap.end()) {
      amap[x] = K++;
      if (x) amap[-x] = K++;
    }
    fence[i] = amap[x];
    fence[n + i] = amap[-x];
  }
  fence[n - 1] = K++;
  scanf("%d", &q);
  for (int i = 0; i < (q); ++i) {
    int l, r;
    scanf("%d %d", &l, &r);
    Q[i].l = l - 1;
    Q[i].r = r - 2;
    Q[i].i = i;
  }
}
bool query_len_cmp(const query& A, const query& B) {
  if (A.len() != B.len())
    return A.len() > B.len();
  else
    return A.i < B.i;
}
int t[MAXN], sufl[MAXN], sufr[MAXN];
bool added[MAXN];
int find(int i) { return t[i] = (t[i] == i ? i : find(t[i])); }
void _union(int i, int j) {
  if (i > j) swap(i, j);
  sufr[find(i)] = sufr[find(j)];
  t[find(j)] = find(i);
}
void addsuf(int i) {
  sufl[i] = i;
  if (i && added[i - 1]) _union(i, i - 1);
  if (added[i + 1]) _union(i, i + 1);
  added[i] = 1;
}
pair<int, int> lcps[MAXN];
void find_lrsufs() {
  for (int i = 0; i < (2 * n - 1); ++i) {
    lcps[i] = make_pair(lcp[i], i);
    t[i] = sufr[i] = i;
    sufl[i] = i + 1;
  }
  sort(lcps, lcps + 2 * n - 1, greater<pair<int, int> >());
  lcps[2 * n - 1] = make_pair(-1, -1);
  sort(Q, Q + q, query_len_cmp);
  int lcpi = 0;
  for (int i = 0; i < (q); ++i) {
    if (Q[i].len() == 0) {
      Q[i].res = n - 1;
      continue;
    }
    while (lcps[lcpi].first >= Q[i].len()) addsuf(lcps[lcpi++].second);
    Q[i].sufl = sufl[find(rsuf[Q[i].l])] - 1;
    Q[i].sufr =
        added[rsuf[Q[i].l] + 1] ? sufr[find(rsuf[Q[i].l] + 1)] : rsuf[Q[i].l];
    Q[i].res = sufcnt[Q[i].sufr] - (Q[i].sufl ? sufcnt[Q[i].sufl - 1] : 0);
  }
}
bool query_i_cmp(const query& A, const query& B) { return A.i < B.i; }
struct op {
  int mn, x, i;
  op() {}
  op(int mn, int x, int i) : mn(mn), x(x), i(i) {}
};
bool operator<(const op& A, const op& B) {
  if (A.x != B.x)
    return A.x < B.x;
  else
    return A.i < B.i;
}
op ops[200000];
int tree[512 * 1024 + 5], cap = 1;
void tinc(int x) {
  x += cap;
  while (x) {
    ++tree[x];
    x /= 2;
  }
}
int tget(int p, int k) {
  int res = 0;
  p += cap;
  k += cap;
  while (p < k) {
    if (p % 2 == 1) res += tree[p];
    if (k % 2 == 0) res += tree[k];
    p = (p + 1) / 2;
    k = (k - 1) / 2;
  }
  if (p == k) res += tree[p];
  return res;
}
void substract_intersections() {
  while (cap < 2 * n - 1) cap *= 2;
  for (int i = 0; i < (q); ++i) {
    ops[2 * i] = op(1, Q[i].l - Q[i].len() - 1, i);
    ops[2 * i + 1] = op(-1, min(n - 2, Q[i].r + 1), i);
  }
  sort(ops, ops + 2 * q);
  int k = 0;
  for (int i = 0; i < (2 * q); ++i) {
    while (k <= ops[i].x) tinc(rsuf[n + k++]);
    if (Q[ops[i].i].len())
      Q[ops[i].i].res += ops[i].mn * tget(Q[ops[i].i].sufl, Q[ops[i].i].sufr);
  }
}
int main() {
  input();
  if (n == 1) {
    for (int i = 0; i < (q); ++i) printf("0\n");
    return 0;
  }
  suffixArray(fence, 2 * n - 1, suf, K);
  for (int i = 0; i < (2 * n - 1); ++i) rsuf[suf[i]] = i;
  computeLCP(suf, rsuf, 2 * n - 1, fence, lcp);
  sufcnt[0] = suf[0] > n - 1;
  for (int i = (1); i < (2 * n - 1); ++i)
    sufcnt[i] = sufcnt[i - 1] + (suf[i] > n - 1);
  find_lrsufs();
  sort(Q, Q + q, query_i_cmp);
  substract_intersections();
  for (int i = 0; i < (q); ++i) printf("%d\n", Q[i].res);
  return 0;
}