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;
struct suffix_array {
  int first, second, idx;
} suff[200000 + 1];
struct query {
  int first, second, l, r, id;
} q[200000 + 1];
pair<int, int> t[200000 + 1];
map<int, int> frecv;
int P[17 + 1][200000 + 1];
int where[200000 + 1];
int res[200000 + 1];
int aib[200000 + 1];
int v[200000 + 1];
int N, Q, T, step;
void Update(int p) {
  while (p <= N) {
    aib[p] += 1;
    p += (p & (-p));
  }
}
int Query(int p) {
  int sum = 0;
  while (p) {
    sum += aib[p];
    p -= (p & (-p));
  }
  return sum;
}
bool cmp(const suffix_array A, const suffix_array B) {
  if (A.first == B.first) return A.second < B.second;
  return A.first < B.first;
}
void Build_suffix() {
  int i, len, cnt = 0;
  for (i = 1; i <= N; ++i) suff[i] = {t[i].first, 0, t[i].second};
  for (step = 0; (1 << step) <= N; ++step) {
    sort(suff + 1, suff + N + 1, cmp);
    P[step][suff[1].idx] = 1;
    for (i = 2; i <= N; ++i)
      if (suff[i].first == suff[i - 1].first &&
          suff[i].second == suff[i - 1].second)
        P[step][suff[i].idx] = P[step][suff[i - 1].idx];
      else
        P[step][suff[i].idx] = P[step][suff[i - 1].idx] + 1;
    len = 1 << step;
    for (i = 1; i <= N; ++i)
      if (i + len <= N)
        suff[i] = {P[step][i], P[step][i + len], i};
      else
        suff[i] = {P[step][i], -1, i};
  }
  for (i = 1; i <= N; ++i) where[P[step - 1][i]] = i;
}
int LCP(int first, int second) {
  int k, len = 0;
  for (k = step - 1; k >= 0 && first <= N && second <= N; --k)
    if (P[k][first] == P[k][second])
      len += (1 << k), first += (1 << k), second += (1 << k);
  return len;
}
int CautBinL(int l, int r) {
  int f, st, dr, mid, len = r - l;
  for (st = 1, dr = P[step - 1][l]; st <= dr;) {
    mid = (st + dr) / 2;
    if (LCP(l, where[mid]) < len)
      st = mid + 1;
    else {
      f = mid;
      dr = mid - 1;
    }
  }
  return f;
}
int CautBinR(int l, int r) {
  int f, st, dr, mid, len = r - l;
  for (st = P[step - 1][l], dr = N; st <= dr;) {
    mid = (st + dr) / 2;
    if (LCP(l, where[mid]) < len)
      dr = mid - 1;
    else {
      f = mid;
      st = mid + 1;
    }
  }
  return f;
}
bool cmp1(const query A, const query B) { return A.r > B.r; }
bool cmp2(const query A, const query B) {
  return 2 * A.l - A.r < 2 * B.l - B.r;
}
int main() {
  int i, j, l, r;
  scanf("%d", &N);
  for (i = 1; i <= N; ++i) scanf("%d", &v[i]);
  for (i = 1; i <= N; ++i) ++frecv[v[i]];
  for (i = 1; i < N; ++i) t[i] = {v[i] - v[i + 1], i};
  for (i = 1; i < N; ++i) t[N + i - 1] = {v[i + 1] - v[i], N + i - 1};
  N = 2 * N - 2;
  sort(t + 1, t + N + 1);
  Build_suffix();
  scanf("%d", &Q);
  for (int t = 1; t <= Q; ++t) {
    scanf("%d %d", &l, &r);
    if (l == r)
      res[t] = N / 2;
    else
      q[++T] = {CautBinL(l, r), CautBinR(l, r), l, r, t};
  }
  sort(q + 1, q + T + 1, cmp1);
  for (j = N, i = 1; i <= T; ++i) {
    while (j > N / 2 + q[i].r) Update(P[step - 1][j--]);
    res[q[i].id] += Query(q[i].second) - Query(q[i].first - 1);
  }
  for (i = 1; i <= N; ++i) aib[i] = 0;
  sort(q + 1, q + T + 1, cmp2);
  for (j = N / 2 + 1, i = 1; i <= T; ++i) {
    while (j < N / 2 + 2 * q[i].l - q[i].r) Update(P[step - 1][j++]);
    res[q[i].id] += Query(q[i].second) - Query(q[i].first - 1);
  }
  for (i = 1; i <= Q; ++i) printf("%d\n", res[i]);
  return 0;
}