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, h[100011], x[400011], m, ql[100011], qr[100011];
deque<pair<int, int>> q1, q2;
void init() {
  scanf("%d", &n);
  for (int i = 1; i <= n; ++i) scanf("%d", &h[i]);
  fill(x, x + 400001, 0x7ffffffe);
  for (int i = 1; i < n; ++i) x[i + n] = -(x[i] = h[i + 1] - h[i]);
  x[n] = 0x7fffffff, m = n + n - 1;
  scanf("%d", &q);
  for (int i = 1; i <= q; ++i) {
    scanf("%d%d", &ql[i], &qr[i]);
    if (ql[i] != qr[i]) {
      if (ql[i] - qr[i] + ql[i] > 1)
        q1.push_back(make_pair(ql[i] - 1 - qr[i] + ql[i], i));
      q2.push_back(make_pair(qr[i] == n ? qr[i] - 1 : qr[i], i));
    }
  }
  sort(q1.begin(), q1.end());
  sort(q2.begin(), q2.end());
}
int sa[200011], rk[400011], _r[200011], _l, _c;
inline bool cmp(int A, int B) {
  return rk[A] < rk[B] || (rk[A] == rk[B] && rk[A + _l] < rk[B + _l]);
}
void suffix_array() {
  memcpy(rk, x, sizeof(rk));
  for (_l = 1; _l < m; _l <<= 1) {
    for (int i = 1; i <= m; ++i) sa[i] = i;
    sort(sa + 1, sa + m + 1, cmp);
    _r[sa[1]] = _c = 1;
    for (int i = 2; i <= m; ++i) _r[sa[i]] = _c += cmp(sa[i - 1], sa[i]);
    for (int i = 1; i <= m; ++i) rk[i] = _r[i];
    if (_c == m) break;
  }
}
int ht[200011], rmq[19][200011];
inline int log2(int first) {
  for (int i = 0;; ++i)
    if (first >> (i + 1) == 0) return i;
}
inline int lcp(int L, int R) {
  int k = log2(R - L + 1);
  return min(rmq[k][L], rmq[k][R - (1 << k) + 1]);
}
void height_lcp() {
  for (int i(1), k(0), j; i <= m; ht[rk[i++]] = k)
    if (rk[i] == 1)
      k = 0;
    else
      for (k -= k > 0, j = sa[rk[i] - 1]; x[i + k] == x[j + k]; ++k)
        ;
  for (int i = 1; i <= m; ++i) rmq[0][i] = ht[i];
  for (int k = 1, d(1); d + d <= m; ++k, d <<= 1)
    for (int i = 1; i + d + d - 1 <= m; ++i)
      rmq[k][i] = min(rmq[k - 1][i], rmq[k - 1][i + d]);
}
int lb[100011], rb[100011], ans[100011];
int find_left_bound(int k, int s) {
  int L(1), R(k), mid;
  while (L < R) {
    mid = (L + R) >> 1;
    lcp(mid + 1, k) < s ? L = mid + 1 : R = mid;
  }
  return L;
}
int find_right_bound(int k, int s) {
  int L(k), R(m), mid;
  while (L < R) {
    mid = (L + R + 1) >> 1;
    lcp(k + 1, mid) < s ? R = mid - 1 : L = mid;
  }
  return L;
}
int f[400011];
void insert(int k, int v) {
  for (; k <= m; k += k & -k) f[k] += v;
}
int query(int p) {
  int v(0);
  for (; p; p -= p & -p) v += f[p];
  return v;
}
void work() {
  for (int i = 1; i <= q; ++i) {
    if (ql[i] == qr[i]) {
      ans[i] = n - 1;
      continue;
    }
    lb[i] = find_left_bound(rk[ql[i] + n], qr[i] - ql[i]);
    rb[i] = find_right_bound(rk[ql[i] + n], qr[i] - ql[i]);
  }
  for (int i = 1; i < n; ++i) {
    insert(rk[i], 1);
    for (; q1.size() && q1[0].first == i; q1.pop_front())
      ans[q1[0].second] +=
          query(rb[q1[0].second]) - query(lb[q1[0].second] - 1);
    for (; q2.size() && q2[0].first == i; q2.pop_front())
      ans[q2[0].second] -=
          query(rb[q2[0].second]) - query(lb[q2[0].second] - 1);
  }
  insert(rk[n], 1);
  for (int i = 1; i <= q; ++i)
    ans[i] += ql[i] != qr[i] ? query(rb[i]) - query(lb[i] - 1) : 0;
  for (int i = 1; i <= q; ++i) printf("%d\n", ans[i]);
}
int main() {
  init();
  suffix_array();
  height_lcp();
  work();
  return 0;
}