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>
clock_t t = clock();
namespace my_std {
using namespace std;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
template <typename T>
inline T rnd(T l, T r) {
  return uniform_int_distribution<T>(l, r)(rng);
}
template <typename T>
inline bool chkmax(T &x, T y) {
  return x < y ? x = y, 1 : 0;
}
template <typename T>
inline bool chkmin(T &x, T y) {
  return x > y ? x = y, 1 : 0;
}
template <typename T>
inline void read(T &t) {
  t = 0;
  char f = 0, ch = getchar();
  double d = 0.1;
  while (ch > '9' || ch < '0') f |= (ch == '-'), ch = getchar();
  while (ch <= '9' && ch >= '0') t = t * 10 + ch - 48, ch = getchar();
  if (ch == '.') {
    ch = getchar();
    while (ch <= '9' && ch >= '0') t += d * (ch ^ 48), d *= 0.1, ch = getchar();
  }
  t = (f ? -t : t);
}
template <typename T, typename... Args>
inline void read(T &t, Args &...args) {
  read(t);
  read(args...);
}
char __sr[1 << 21], __z[20];
int __C = -1, __zz = 0;
inline void Ot() { fwrite(__sr, 1, __C + 1, stdout), __C = -1; }
inline void print(register int x) {
  if (__C > 1 << 20) Ot();
  if (x < 0) __sr[++__C] = '-', x = -x;
  while (__z[++__zz] = x % 10 + 48, x /= 10)
    ;
  while (__sr[++__C] = __z[__zz], --__zz)
    ;
  __sr[++__C] = '\n';
}
void file() {}
inline void chktime() {}
long long ksm(long long x, int y) {
  long long ret = 1;
  for (; y; y >>= 1, x = x * x)
    if (y & 1) ret = ret * x;
  return ret;
}
}  // namespace my_std
using namespace my_std;
int n;
int a[202020], aa[202020];
int h[202020];
namespace SA {
int sa[202020], rnk[202020], _rnk[202020], tp[202020], cnt[202020];
int n;
void Sort(int m) {
  for (int i = (1); i <= (m); i++) cnt[i] = 0;
  for (int i = (1); i <= (n); i++) ++cnt[rnk[i]];
  for (int i = (1); i <= (m); i++) cnt[i] += cnt[i - 1];
  for (int i = (n); i >= (1); i--) sa[cnt[rnk[tp[i]]]--] = tp[i];
}
void init() {
  for (int i = (1); i <= (n); i++) rnk[i] = a[i], tp[i] = i;
  Sort(n + 10);
  for (int p = 0, w = 1, m = n + 10; p < n; m = p, w <<= 1) {
    p = 0;
    for (int i = (1); i <= (w); i++) tp[++p] = n - i + 1;
    for (int i = (1); i <= (n); i++)
      if (sa[i] > w) tp[++p] = sa[i] - w;
    Sort(m);
    memcpy(_rnk, rnk, sizeof(rnk));
    rnk[sa[p = 1]] = 1;
    for (int i = (2); i <= (n); i++)
      rnk[sa[i]] = (_rnk[sa[i]] == _rnk[sa[i - 1]] &&
                    _rnk[sa[i] + w] == _rnk[sa[i - 1] + w])
                       ? p
                       : ++p;
  }
  for (int i = 1, k = 0; i <= n; i++) {
    if (k) --k;
    int j = sa[rnk[i] - 1];
    while (a[i + k] == a[j + k]) ++k;
    h[rnk[i]] = k;
  }
}
}  // namespace SA
int tr[202020 * 20], ls[202020 * 20], rs[202020 * 20], cnt;
int root[202020];
void insert(int &k, int l, int r, int x, int pre) {
  k = ++cnt;
  tr[k] = tr[pre] + 1;
  ls[k] = ls[pre];
  rs[k] = rs[pre];
  if (l == r) return;
  int mid = (l + r) >> 1;
  if (x <= mid)
    insert(ls[k], l, mid, x, ls[pre]);
  else
    insert(rs[k], mid + 1, r, x, rs[pre]);
}
int query(int k, int l, int r, int x, int y) {
  if (!k || x > y) return 0;
  if (x <= l && r <= y) return tr[k];
  int mid = (l + r) >> 1, ret = 0;
  if (x <= mid) ret += query(ls[k], l, mid, x, y);
  if (y > mid) ret += query(rs[k], mid + 1, r, x, y);
  return ret;
}
int query(int rnk, int l, int r) { return query(root[rnk], 1, n, l, r); }
int query(int rnk1, int rnk2, int l, int r) {
  if (rnk1 > rnk2) return 0;
  return query(rnk2, l, r) - query(rnk1 - 1, l, r);
}
int st[202020][25], lg2[202020];
void init() {
  int N = n + n - 1;
  for (int i = (1); i <= (N); i++) st[i][0] = h[i];
  for (int i = (2); i <= (N); i++) lg2[i] = lg2[i >> 1] + 1;
  for (int i = (1); i <= (N); i++)
    if (SA::sa[i] > n)
      insert(root[i], 1, n, SA::sa[i] - n, root[i - 1]);
    else
      root[i] = root[i - 1];
  for (int j = (1); j <= (20); j++)
    for (int i = (1); i <= (N - (1 << j) + 1); i++)
      st[i][j] = min(st[i][j - 1], st[i + (1 << (j - 1))][j - 1]);
}
int query(int l, int r) {
  if (l == r) return 1e9;
  if (l > r) swap(l, r);
  ++l;
  int len = lg2[r - l + 1];
  return min(st[l][len], st[r - (1 << len) + 1][len]);
}
int Query(int p, int len, int type) {
  p = SA::rnk[p];
  int ret = p, l = 1, r = n + n - 1;
  if (type)
    l = p;
  else
    r = p;
  while (l <= r) {
    int mid = (l + r) >> 1;
    if (query(p, mid) >= len)
      ret = mid, (type ? l = mid + 1 : r = mid - 1);
    else
      (type ? r = mid - 1 : l = mid + 1);
  }
  return ret;
}
int main() {
  file();
  read(n);
  for (int i = (1); i <= (n); i++) read(aa[i]);
  for (int i = (1); i <= (n - 1); i++)
    aa[i] = aa[i + 1] - aa[i], a[i] = aa[i], aa[i + n] = a[i + n] = -aa[i];
  a[n] = aa[n] = 2e9;
  sort(aa + 1, aa + n + n);
  int _ = unique(aa + 1, aa + n + n) - aa - 1;
  for (int i = (1); i <= (n + n - 1); i++)
    a[i] = lower_bound(aa + 1, aa + _ + 1, a[i]) - aa;
  SA::n = n + n - 1;
  SA::init();
  init();
  int Q;
  read(Q);
  int x, y, l, r;
  while (Q--) {
    read(x, y);
    --y;
    if (x > y) {
      printf("%d\n", n - 1);
      continue;
    }
    l = Query(x, y - x + 1, 0);
    r = Query(x, y - x + 1, 1);
    int ans = 0, len = y - x + 1, t = SA::rnk[x];
    ans += query(l, t - 1, 1, x - len - 1) + query(l, t - 1, y + 1 + 1, n - 1);
    ans += query(t + 1, r, 1, x - len - 1) + query(t + 1, r, y + 1 + 1, n - 1);
    printf("%d\n", ans);
  }
  return 0;
}