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;
int x, l, r, n, Q, t, cnt, last, ma[400005], pr[400005], f[400005][20],
    ans[100005], a[200005], e[400005], P[200005];
vector<int> u[400005], c[400005];
map<int, int> ch[400005];
struct ss {
  int i, x;
} A[200005];
struct bla {
  int l, r, s;
} b[1800000];
bool cmp(ss a, ss b) { return a.x < b.x; }
void add(int c) {
  int p = last, np = last = ++cnt, q, nq;
  ma[np] = ma[p] + 1;
  while (p && !ch[p][c]) ch[p][c] = np, p = pr[p];
  if (!p)
    pr[np] = 1;
  else {
    q = ch[p][c];
    if (ma[q] == ma[p] + 1)
      pr[np] = q;
    else {
      ma[nq = ++cnt] = ma[p] + 1;
      pr[nq] = pr[q];
      pr[q] = pr[np] = nq;
      ch[nq] = ch[q];
      while (ch[p][c] == q) ch[p][c] = nq, p = pr[p];
    }
  }
}
void dfs(int x, int y) {
  f[x][0] = y;
  for (int i = 0; i < 20; ++i)
    if (!f[f[x][i]][i])
      break;
    else
      f[x][i + 1] = f[f[x][i]][i];
  for (int i = 0; i < (int)u[x].size(); ++i) dfs(u[x][i], x);
}
void join(int &u, int p, int q, int x) {
  if (!u) u = ++t;
  ++b[u].s;
  if (p == q) return;
  if (x <= p + q >> 1)
    join(b[u].l, p, p + q >> 1, x);
  else
    join(b[u].r, (p + q >> 1) + 1, q, x);
}
void he(int &u, int v) {
  if (!u) {
    b[u = v] = b[v];
    return;
  }
  if (!v) return;
  b[u].s += b[v].s;
  he(b[u].l, b[v].l);
  he(b[u].r, b[v].r);
}
int qiu(int u, int p, int q, int l, int r) {
  if (!u) return 0;
  if (l <= p && q <= r) return b[u].s;
  int s = 0;
  if (l <= p + q >> 1) s = qiu(b[u].l, p, p + q >> 1, l, r);
  if (r > p + q >> 1) s += qiu(b[u].r, (p + q >> 1) + 1, q, l, r);
  return s;
}
void fly(int x) {
  for (int i = 0; i < (int)u[x].size(); ++i) {
    fly(u[x][i]);
    he(e[x], e[u[x][i]]);
  }
  for (int i = 0; i < (int)c[x].size(); ++i) {
    int l = A[c[x][i]].i, r = A[c[x][i]].x, p, q;
    p = 1 + r - l, q = l - 1;
    if (p <= q) ans[c[x][i]] += qiu(e[x], 1, n, p, q);
    p = r + 1 + r - l, q = n;
    if (p <= q) ans[c[x][i]] += qiu(e[x], 1, n, p, q);
  }
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; ++i) scanf("%d", &a[i]);
  for (int i = n; i; --i) a[i] -= a[i - 1];
  for (int i = 1; i <= n; ++i) a[i + n] = -a[i];
  for (int i = 1; i <= n + n; ++i) A[i] = (ss){i, a[i]};
  sort(A + 1, A + n + n + 1, cmp);
  for (int i = 1; i <= n + n; ++i) {
    if (i == 1 || A[i].x != A[i - 1].x) ++cnt;
    a[A[i].i] = cnt;
  }
  cnt = last = 1;
  for (int i = 1; i <= n + n; ++i) add(a[i]), P[i] = last;
  for (int i = 2; i <= cnt; ++i) u[pr[i]].push_back(i);
  dfs(1, 0);
  t = 0;
  for (int i = n + 1; i <= n + n; ++i) join(e[P[i]], 1, n, i - n);
  scanf("%d", &Q);
  for (int i = 1; i <= Q; ++i) {
    scanf("%d%d", &l, &r);
    if (l == r) {
      ans[i] = n - 1;
      continue;
    }
    x = P[r];
    A[i].i = l, A[i].x = r;
    for (int j = 19; ~j; --j)
      if (f[x][j] && r - l <= ma[f[x][j]]) x = f[x][j];
    c[x].push_back(i);
  }
  fly(1);
  for (int i = 1; i <= Q; ++i) printf("%d\n", ans[i]);
  return 0;
}