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;
template <class T>
inline void read(T& x) {
  bool fu = 0;
  char c;
  for (c = getchar(); c <= 32; c = getchar())
    ;
  if (c == '-') fu = 1, c = getchar();
  for (x = 0; c > 32; c = getchar()) x = x * 10 + c - '0';
  if (fu) x = -x;
};
template <class T>
inline void read(T& x, T& y) {
  read(x);
  read(y);
}
template <class T>
inline void read(T& x, T& y, T& z) {
  read(x);
  read(y);
  read(z);
}
template <class T>
inline void read(T& x, T& y, T& z, T& q) {
  read(x);
  read(y);
  read(z);
  read(q);
}
inline char getc() {
  char c;
  for (c = getchar(); c <= 32; c = getchar())
    ;
  return c;
}
const int N = 1000010;
const int inf = int(2e9);
int g, q, n, n0, m, i, j, k, l, now, p, x, y, tt;
int A[N], a[N], o[N], ans[N];
struct aa {
  int pos, x, l, r, k;
} ask[N];
bool cmp(aa a, aa b) { return a.x < b.x; }
int sa[N], Ra[N], sa2[N], Ra2[N], h[N], sum[N], two[N], H[N][21];
void preRMQ() {
  int i, j;
  two[0] = 0;
  for (i = 0; i <= 20; i++)
    for (j = (1 << i); j <= (1 << (i + 1)) - 1; j++)
      if (j > n)
        break;
      else
        two[j] = i;
  for (i = n; i >= 1; i--) {
    H[i][0] = h[i];
    for (j = 1; j <= two[n - i + 1]; j++)
      H[i][j] = min(H[i][j - 1], H[i + (1 << (j - 1))][j - 1]);
  }
}
inline int lcp(int a, int b) {
  if (a == b) return inf;
  if (a > b) swap(a, b);
  a++;
  return min(H[a][two[b - a + 1]],
             H[b - (1 << two[b - a + 1]) + 1][two[b - a + 1]]);
}
void suffix() {
  int i, j, p, x, y;
  memset(sum, 0, sizeof(sum));
  for (i = 1; i <= n; i++) ++sum[a[i]];
  for (i = 1; i <= n; i++) sum[i] += sum[i - 1];
  for (i = n; i >= 1; i--) sa[sum[a[i]]--] = i;
  Ra[sa[1]] = 1;
  j = 1;
  for (i = 2; i <= n; i++) {
    if (a[sa[i]] != a[sa[i - 1]]) j++;
    Ra[sa[i]] = j;
  }
  for (p = 1; j < n; p *= 2) {
    j = 0;
    memset(sum, 0, sizeof(sum));
    memcpy(Ra2, Ra, sizeof(Ra));
    for (i = n - p + 1; i <= n; i++) sa2[++j] = i;
    for (i = 1; i <= n; i++)
      if (sa[i] > p) sa2[++j] = sa[i] - p;
    for (i = 1; i <= n; i++) ++sum[Ra2[sa2[i]]];
    for (i = 1; i <= n; i++) sum[i] += sum[i - 1];
    for (i = n; i >= 1; i--) sa[sum[Ra2[sa2[i]]]--] = sa2[i];
    Ra[sa[1]] = 1;
    j = 1;
    for (i = 2; i <= n; i++) {
      if (Ra2[sa[i]] != Ra2[sa[i - 1]] || Ra2[sa[i] + p] != Ra2[sa[i - 1] + p])
        j++;
      Ra[sa[i]] = j;
    }
  }
  h[1] = 0;
  p = 0;
  for (i = 1; i <= n; i++)
    if (Ra[i] > 1) {
      j = sa[Ra[i] - 1];
      while (a[j + p] == a[i + p]) p++;
      h[Ra[i]] = p;
      if (p) p--;
    }
  preRMQ();
}
void find(int i, int t) {
  int l, r, L, R, m;
  for (l = 1, r = i; l < r;) {
    m = (l + r) / 2;
    if (lcp(m, i) >= t)
      r = m;
    else
      l = m + 1;
  }
  L = l;
  for (l = i, r = n; l < r;) {
    m = (l + r + 1) / 2;
    if (lcp(i, m) >= t)
      l = m;
    else
      r = m - 1;
  }
  R = l;
  int z0 = x - t - 1, z1 = x + t + 1;
  if (z0 >= 1) {
    ask[++tt] = (aa){now, L - 1, 1, z0, -1};
    ask[++tt] = (aa){now, R, 1, z0, 1};
  }
  if (z1 <= n0) {
    ask[++tt] = (aa){now, L - 1, z1, n0, -1};
    ask[++tt] = (aa){now, R, z1, n0, 1};
  }
}
int ss[N];
void add(int i) {
  for (; i <= n; i += i & -i) ss[i]++;
}
int S(int i) {
  if (i > n) i = n;
  if (i < 0) i = 0;
  int ans = 0;
  for (; i; i -= i & -i) ans += ss[i];
  return ans;
}
int main() {
  read(n);
  for (i = 1; i <= n; i++) read(A[i]);
  for (i = 1; i <= n - 1; i++) a[++g] = A[i + 1] - A[i];
  a[++g] = inf;
  for (i = 1; i <= n - 1; i++) a[++g] = -(A[i + 1] - A[i]);
  for (i = 1; i <= g; i++) o[i] = a[i];
  sort(o + 1, o + 1 + g);
  j = 1;
  for (i = 2; i <= g; i++)
    if (o[i] != o[i - 1]) o[++j] = o[i];
  for (i = 1; i <= g; i++) a[i] = lower_bound(o + 1, o + 1 + j, a[i]) - o;
  n0 = n;
  n = g;
  suffix();
  read(q);
  for (now = 1; now <= q; now++) {
    read(x, y);
    if (x == y)
      ans[now] = n0 - 1;
    else
      find(Ra[n0 + x], y - x);
  }
  sort(ask + 1, ask + 1 + tt, cmp);
  p = 1;
  for (i = 1; i <= tt; i++) {
    for (; p <= n && p <= ask[i].x; p++) add(sa[p]);
    ans[ask[i].pos] += ask[i].k * (S(ask[i].r) - S(ask[i].l - 1));
  }
  for (i = 1; i <= q; i++) printf("%d\n", ans[i]);
  return 0;
}