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 N, Q, T, len, md, ans;
int rk[200005], x[200005], y[200005];
int Sa[200005], tg[200005];
int ht[200005], mn[200005][19], lg[200005];
int p[200005], ss[200005];
int root[200005], sum[5000005], son[5000005][2];
int l1, r1, cnt;
int ql, qr, mid, pt;
map<int, int> hash;
int Insert(int now, int l, int r) {
  int mid = (l + r) / 2, tmp = ++pt;
  sum[tmp] = sum[now] + 1;
  son[tmp][0] = son[now][0], son[tmp][1] = son[now][1];
  if (l == r) return tmp;
  if (ql <= mid)
    son[tmp][0] = Insert(son[now][0], l, mid);
  else
    son[tmp][1] = Insert(son[now][1], mid + 1, r);
  return tmp;
}
int Query(int now, int l, int r) {
  int mid = (l + r) / 2, get = 0;
  if (ql <= l && qr >= r) return sum[now];
  if (ql <= mid) get += Query(son[now][0], l, mid);
  if (qr > mid) get += Query(son[now][1], mid + 1, r);
  return get;
}
int Ask(int l, int r) {
  int k = lg[r - l + 1];
  if (l > r) return 0x7fffffff;
  return min(mn[l][k], mn[r - (1 << k) + 1][k]);
}
int find(int x) {
  int l, r, mid;
  l = 1, r = T;
  while (l <= r) {
    mid = (l + r) / 2;
    if (rk[mid] >= x)
      r = mid - 1;
    else
      l = mid + 1;
  }
  return l;
}
void tgsort(int fr[], int by[], int to[], int n, int m) {
  int i;
  for (i = 0; i <= m; i++) tg[i] = 0;
  for (i = 1; i <= n; i++) tg[by[i]]++;
  for (i = 1; i <= m; i++) tg[i] += tg[i - 1];
  for (i = n; i >= 1; i--) to[tg[by[fr[i]]]--] = fr[i];
}
void find_Sa() {
  int i, j, t;
  for (i = 1; i <= len; i++) rk[i] = i;
  tgsort(rk, ss, Sa, len, T);
  for (i = 1; i <= len; i++)
    rk[Sa[i]] = rk[Sa[i - 1]] + (ss[Sa[i]] != ss[Sa[i - 1]]);
  for (t = 0; rk[Sa[len]] < len; t++) {
    for (i = 1; i <= len; i++)
      x[i] = rk[i], y[i] = (i + (1 << t) <= len) ? rk[i + (1 << t)] : 0;
    for (i = 1; i <= len; i++) Sa[i] = i;
    tgsort(Sa, y, rk, len, len);
    tgsort(rk, x, Sa, len, len);
    for (i = 1; i <= len; i++)
      rk[Sa[i]] = rk[Sa[i - 1]] +
                  ((x[Sa[i]] != x[Sa[i - 1]]) || (y[Sa[i]] != y[Sa[i - 1]]));
  }
}
void find_Ht() {
  int i, j = 0;
  for (i = 1; i <= len; i++) {
    if (j) j--;
    if (rk[i] == 1) {
      ht[rk[i]] = -1;
      continue;
    }
    for (j; ss[i + j] == ss[Sa[rk[i] - 1] + j]; j++)
      ;
    ht[rk[i]] = j, j = max(0, j - 1);
  }
  for (i = 1; i <= len; i++) mn[i][0] = ht[i];
  for (i = 1; i <= 18; i++)
    for (j = 1; j + (1 << i) <= len; j++)
      mn[j][i] = min(mn[j][i - 1], mn[j + (1 << i - 1)][i - 1]);
  for (i = 1; i <= len; i++) {
    if (Sa[i] <= md) {
      root[i] = root[i - 1];
      continue;
    }
    ql = Sa[i] - md;
    root[i] = Insert(root[i - 1], 1, N);
  }
  lg[1] = 0;
  for (i = 2; i <= len; i++)
    lg[i] = lg[i - 1] + (i - 1 == (1 << lg[i - 1] + 1));
}
int main() {
  int i, j, tmp, lt, rt, mid;
  int a, b;
  scanf("%d", &N);
  for (i = 1; i <= N; i++) scanf("%d", &p[i]);
  for (i = 1; i < N; i++) ss[++len] = p[i + 1] - p[i];
  ss[md = ++len] = -0x7fffffff;
  for (i = 1; i < N; i++) ss[++len] = p[i] - p[i + 1];
  ss[++len] = 0x7fffffff;
  for (i = 1; i <= len; i++) rk[i] = ss[i];
  sort(rk + 1, rk + 1 + len);
  for (i = 1, T = 0; i <= len; i++)
    if (i == 1 || rk[i] != rk[i - 1]) rk[++T] = rk[i];
  for (i = 1; i <= len; i++) ss[i] = find(ss[i]);
  find_Sa();
  find_Ht();
  scanf("%d", &Q);
  for (i = 1; i <= Q; i++) {
    scanf("%d %d", &l1, &r1);
    if (N == 1)
      ans = 0;
    else {
      cnt = r1 - l1, tmp = rk[l1];
      lt = 1, rt = tmp;
      while (lt <= rt) {
        mid = (lt + rt) / 2;
        if (Ask(mid + 1, tmp) >= cnt)
          rt = mid - 1;
        else
          lt = mid + 1;
      }
      a = lt;
      lt = tmp + 1, rt = len;
      while (lt <= rt) {
        mid = (lt + rt) / 2;
        if (Ask(tmp + 1, mid) >= cnt)
          lt = mid + 1;
        else
          rt = mid - 1;
      }
      b = rt;
      ql = 1, qr = l1 - cnt - 1, ans = 0;
      if (ql <= qr) ans += Query(root[b], 1, N) - Query(root[a - 1], 1, N);
      ql = r1 + 1, qr = N;
      if (ql <= qr) ans += Query(root[b], 1, N) - Query(root[a - 1], 1, N);
    }
    printf("%d\n", ans);
  }
  return 0;
}