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;
void gtmn(int &a, int b) {
  if (a > b) a = b;
}
const int sz = 262143;
int n, m, q;
int s[200010];
int sa[200010], rnk[200010], hei[200010];
int a[100010], cf[100010], rcf[100010];
int ssz[524299];
int mnn[524299];
vector<int> wav[524299];
int cur;
bool sme[200010];
inline bool cmp(int i, int j) {
  if (rnk[i] != rnk[j]) return rnk[i] < rnk[j];
  if (i + cur > m) return 1;
  if (j + cur > m) return 0;
  return rnk[i + cur] < rnk[j + cur];
}
inline bool equ(int i, int j) {
  if (rnk[i] != rnk[j]) return 0;
  if (i + cur > m || j + cur > m) return 0;
  if (rnk[i + cur] != rnk[j + cur]) return 0;
  return 1;
}
int query(int l, int r) {
  int res = mnn[0];
  while (l <= r) {
    if (l & 1) {
      gtmn(res, mnn[l++]);
    }
    if (!(r & 1)) {
      gtmn(res, mnn[r--]);
    }
    l >>= 1;
    r >>= 1;
  }
  return res;
}
int invec(vector<int> &aa, int lt, int rt) {
  return upper_bound(aa.begin(), aa.end(), rt) -
         lower_bound(aa.begin(), aa.end(), lt);
}
int cont(int l, int r, int lt, int rt) {
  if (l > r) return 0;
  if (lt > rt) return 0;
  int res = 0;
  while (l <= r) {
    if (l & 1) {
      res += invec(wav[l], lt, rt);
      ++l;
    }
    if (!(r & 1)) {
      res += invec(wav[r], lt, rt);
      --r;
    }
    l >>= 1;
    r >>= 1;
  }
  return res;
}
void suf() {
  for (int i = 1; i <= m; ++i) {
    sa[i] = i;
    rnk[i] = s[i];
  }
  cur = 1;
  while (cur <= m << 1) {
    sort(&sa[1], &sa[m + 1], cmp);
    memset(sme, 0, sizeof(sme));
    for (int i = 2; i <= m; ++i) {
      if (equ(sa[i - 1], sa[i])) sme[i] = 1;
    }
    rnk[sa[1]] = 1;
    for (int i = 2; i <= m; ++i) {
      if (sme[i]) {
        rnk[sa[i]] = rnk[sa[i - 1]];
      } else {
        rnk[sa[i]] = rnk[sa[i - 1]] + 1;
      }
    }
    cur <<= 1;
  }
  int hh = 0;
  for (int i = 1; i <= m; ++i) {
    if (hh) --hh;
    if (rnk[i] == 1) continue;
    while (sa[rnk[i] - 1] + hh <= m && i + hh <= m &&
           s[sa[rnk[i] - 1] + hh] == s[i + hh]) {
      ++hh;
    }
    hei[i] = hh;
  }
}
int main(void) {
  memset(mnn, 127, sizeof(mnn));
  scanf("%d", &n);
  m = (n << 1) - 1;
  scanf("%d", &a[1]);
  for (int i = 2; i <= n; ++i) {
    scanf("%d", &a[i]);
    cf[i - 1] = a[i] - a[i - 1];
    rcf[i - 1] = -cf[i - 1];
    s[i - 1] = cf[i - 1];
    s[i + n - 1] = rcf[i - 1];
  }
  s[n] = mnn[0];
  suf();
  for (int i = 1; i <= m; ++i) {
    mnn[i + sz] = hei[sa[i]];
  }
  for (int i = sz; i; --i) {
    gtmn(mnn[i], mnn[i << 1]);
    gtmn(mnn[i], mnn[i << 1 | 1]);
  }
  for (int i = 1; i <= m; ++i) {
    wav[i + sz].push_back(sa[i]);
    ssz[i + sz] = 1;
  }
  for (int i = sz; i; --i) {
    int as = ssz[i << 1], bs = ssz[i << 1 | 1];
    ssz[i] = as + bs;
    if (!bs) {
      wav[i] = wav[i << 1];
      continue;
    }
    int ii = 0, jj = 0;
    while (ii < as || jj < bs) {
      if (ii == as || (jj < bs && wav[i << 1][ii] > wav[i << 1 | 1][jj])) {
        wav[i].push_back(wav[i << 1 | 1][jj]);
        ++jj;
      } else {
        wav[i].push_back(wav[i << 1][ii]);
        ++ii;
      }
    }
  }
  scanf("%d", &q);
  while (q--) {
    int l, r;
    scanf("%d%d", &l, &r);
    if (l == r) {
      printf("%d\n", n - 1);
      continue;
    }
    int cur = r - l;
    int lcan = n + l - cur - 1, rcan = n + r + 1;
    int lr, rr;
    int lt = 1, rt = rnk[l], md;
    while (lt + 1 < rt) {
      md = (lt + rt) >> 1;
      if (query(md + sz + 1, rnk[l] + sz) >= cur) {
        rt = md;
      } else {
        lt = md;
      }
    }
    lr = max(rt - 3, 1);
    while (query(lr + 1 + sz, rnk[l] + sz) < cur) ++lr;
    lt = rnk[l], rt = m + 1;
    while (lt + 1 < rt) {
      md = (lt + rt) >> 1;
      if (query(rnk[l] + sz + 1, md + sz) >= cur) {
        lt = md;
      } else {
        rt = md;
      }
    }
    rr = min(lt + 3, m);
    while (query(rnk[l] + 1 + sz, rr + sz) < cur) --rr;
    printf("%d\n", cont(lr + sz, rr + sz, n + 1, lcan) +
                       cont(lr + sz, rr + sz, rcan, m));
  }
  return 0;
}