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>
int a[400001], sa[400001], rank[400001], tax[2000001], tp[400001], n, m, q,
    Log[400001];
int ans[400001];
int st[200001][21];
void Qsort() {
  for (int i = 0; i <= m; i++) tax[i] = 0;
  for (int i = 1; i <= n; i++) ++tax[rank[i]];
  for (int i = 1; i <= m; i++) tax[i] += tax[i - 1];
  for (int i = n; i; i--) sa[tax[rank[tp[i]]]--] = tp[i];
}
void init() {
  for (int i = 1; i <= n; i++) rank[i] = a[i], tp[i] = i;
  Qsort();
  for (int p = 1, w = 1; p < n; m = p, w <<= 1) {
    p = 0;
    for (int i = 1; i <= w; i++) tp[++p] = n - w + i;
    for (int i = 1; i <= n; i++)
      if (sa[i] > w) tp[++p] = sa[i] - w;
    Qsort();
    std::swap(rank, tp);
    rank[sa[1]] = p = 1;
    for (int i = 2; i <= n; i++)
      rank[sa[i]] =
          (tp[sa[i]] == tp[sa[i - 1]] && tp[sa[i] + w] == tp[sa[i - 1] + w])
              ? p
              : ++p;
  }
  for (int i = 1, k = 0; i <= n; i++) {
    if (k) --k;
    if (rank[i] == n) continue;
    while (i + k <= n && a[i + k] == a[sa[rank[i] + 1] + k]) ++k;
    st[rank[i]][0] = k;
  }
  for (int j = 1; (1 << j) < n; j++)
    for (int i = 1; i + (1 << j) - 1 < n; i++)
      st[i][j] = std::min(st[i][j - 1], st[i + (1 << (j - 1))][j - 1]);
}
int query(int l, int r) {
  --r;
  int k = Log[r - l + 1];
  return std::min(st[l][k], st[r - (1 << k) + 1][k]);
}
int c[400001];
void update(int ind) {
  for (; ind <= n; ind += ind & -ind) ++c[ind];
}
int Query(int ind) {
  int tot = 0;
  for (; ind; ind -= ind & -ind) tot += c[ind];
  return tot;
}
struct que {
  int l, r, ord, v;
};
std::vector<que> vec[400001];
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) scanf("%d", a + i);
  for (int i = 1; i < n; i++) a[i] = a[i + 1] - a[i];
  --n;
  for (int i = 1; i <= n; i++) a[i + n] = -a[i];
  memcpy(tp, a, sizeof tp);
  n <<= 1;
  std::sort(tp + 1, tp + n + 1);
  m = std::unique(tp + 1, tp + n + 1) - tp - 1;
  for (int i = 1; i <= n; i++)
    a[i] = std::lower_bound(tp + 1, tp + m + 1, a[i]) - tp;
  for (int i = 2; i <= n; i++) Log[i] = Log[i >> 1] + 1;
  init();
  scanf("%d", &q);
  n = (n >> 1) + 1;
  for (int i = 1, l, r; i <= q; i++) {
    scanf("%d%d", &l, &r);
    if (l == r) {
      ans[i] = n - 1;
      continue;
    }
    int L = rank[l + n - 1], R = rank[l + n - 1];
    for (int j = 20; ~j; --j)
      if (L - (1 << j) >= 1 && query(L - (1 << j), rank[l + n - 1]) >= r - l)
        L -= 1 << j;
    for (int j = 20; ~j; --j)
      if (R + (1 << j) <= ((n - 1) << 1) &&
          query(rank[l + n - 1], R + (1 << j)) >= r - l)
        R += 1 << j;
    vec[L - 1].push_back((que){1, (l << 1) - r - 1, i, -1});
    vec[L - 1].push_back((que){r + 1, n - r + l, i, -1});
    vec[R].push_back((que){1, (l << 1) - r - 1, i, 1});
    vec[R].push_back((que){r + 1, n - r + l, i, 1});
  }
  for (int i = 1; i <= ((n - 1) << 1); i++) {
    if (sa[i] >= 1 && sa[i] < n) update(sa[i]);
    for (int j = 0; j < vec[i].size(); j++) {
      int l = vec[i][j].l, r = vec[i][j].r;
      if (l > r) continue;
      ans[vec[i][j].ord] += vec[i][j].v * (Query(r) - Query(l - 1));
    }
  }
  for (int i = 1; i <= q; i++) printf("%d\n", ans[i]);
}