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 m, Q, cnt, top;
int a[200005], b[200005], c[200005];
int v[200005], sa[2][200005], rk[2][200005], h[200005], p, q;
int mn[200005][19], lg[200005];
struct node {
  int l, r, id;
  bool operator<(const node &a) const { return l + r < a.l + a.r; }
} qq[200005];
void mul(int k, int *sa, int *rk, int *SA, int *RK) {
  for (int(i) = (1); (i) <= (cnt); (i)++) v[rk[sa[i]]] = i;
  for (int i = cnt; i; i--)
    if (sa[i] > k) SA[v[rk[sa[i] - k]]--] = sa[i] - k;
  for (int(i) = (cnt - k + 1); (i) <= (cnt); (i)++) SA[v[rk[i]]--] = i;
  for (int(i) = (1); (i) <= (cnt); (i)++)
    RK[SA[i]] = RK[SA[i - 1]] + (rk[SA[i]] != rk[SA[i - 1]] ||
                                 rk[SA[i - 1] + k] != rk[SA[i] + k]);
}
void get_sa(int lim) {
  p = 1;
  for (int(i) = (1); (i) <= (cnt); (i)++) v[b[i]]++;
  for (int(i) = (1); (i) <= (lim); (i)++) v[i] += v[i - 1];
  for (int(i) = (1); (i) <= (cnt); (i)++) sa[p][v[b[i]]--] = i;
  for (int(i) = (1); (i) <= (cnt); (i)++)
    rk[p][sa[p][i]] = rk[p][sa[p][i - 1]] + (b[sa[p][i]] != b[sa[p][i - 1]]);
  for (int k = 1; k <= cnt; k <<= 1) {
    mul(k, sa[p], rk[p], sa[q], rk[q]);
    swap(p, q);
  }
  for (int j, k = 0, i = 1; i <= cnt; i++) {
    j = sa[p][rk[p][i] - 1];
    while (b[i + k] == b[j + k]) k++;
    h[rk[p][i]] = k;
    if (k) k--;
  }
}
void add(int x, int y) {
  for (; x <= cnt; x += (x & (-x))) c[x] += y;
}
int query(int x) {
  int ret = 0;
  for (; x; x -= (x & (-x))) ret += c[x];
  return ret;
}
int get_mn(int l, int r) {
  int t = lg[r - l + 1];
  return min(mn[l][t], mn[r - (1 << t) + 1][t]);
}
int get_ans(int pos, int k) {
  int tl = pos + 1, tr = pos - 1;
  int l = pos + 1, r = cnt, mid;
  while (l <= r) {
    mid = (l + r) >> 1;
    if (get_mn(pos + 1, mid) >= k)
      l = mid + 1, tr = mid;
    else
      r = mid - 1;
  }
  r = pos, l = 2;
  while (l <= r) {
    mid = (l + r) >> 1;
    if (get_mn(mid, pos) >= k)
      tl = mid, r = mid - 1;
    else
      l = mid + 1;
  }
  return query(tr) - query(tl - 2);
}
int ans[200005];
bool cmp1(const node &a, const node &b) { return a.l - a.r < b.l - b.r; }
int main() {
  cin >> m;
  for (int(i) = (1); (i) <= (m); (i)++) {
    scanf("%d", &a[i]);
    if (i > 1) b[++cnt] = a[i] - a[i - 1];
  }
  b[++cnt] = 0x3f3f3f3f;
  for (int(i) = (2); (i) <= (m); (i)++) b[++cnt] = a[i - 1] - a[i];
  for (int(i) = (1); (i) <= (cnt); (i)++) c[i] = b[i];
  sort(c + 1, c + cnt + 1);
  top = unique(c + 1, c + cnt + 1) - c - 1;
  for (int(i) = (1); (i) <= (cnt); (i)++)
    b[i] = lower_bound(c + 1, c + top + 1, b[i]) - c;
  get_sa(top);
  for (int(i) = (1); (i) <= (cnt); (i)++) mn[i][0] = h[i];
  for (int j = 1; (1 << j) <= cnt; j++)
    for (int i = 1; i + (1 << j) - 1 <= cnt; i++)
      mn[i][j] = min(mn[i][j - 1], mn[i + (1 << j - 1)][j - 1]);
  for (int i = 2; i <= cnt; i++) lg[i] = lg[i >> 1] + 1;
  cin >> Q;
  for (int i = 1; i <= Q; i++) {
    scanf("%d%d", &qq[i].l, &qq[i].r);
    qq[i].r -= qq[i].l;
    qq[i].id = i;
  }
  sort(qq + 1, qq + Q + 1);
  memset(c, 0, sizeof(c));
  for (int j = m - 1, i = Q; i; i--) {
    while (j > 1 && j > qq[i].l + qq[i].r) add(rk[p][j + m], 1), j--;
    ans[qq[i].id] = get_ans(rk[p][qq[i].l], qq[i].r);
  }
  sort(qq + 1, qq + Q + 1, cmp1);
  memset(c, 0, sizeof(c));
  for (int j = 1, i = 1; i <= Q; i++) {
    while (j < m && j + qq[i].r < qq[i].l) add(rk[p][j + m], 1), j++;
    ans[qq[i].id] += get_ans(rk[p][qq[i].l], qq[i].r);
    if (qq[i].r == 0) ans[qq[i].id] = m - 1;
  }
  for (int i = 1; i <= Q; i++) printf("%d\n", ans[i]);
  return 0;
}