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;
const int MAXN = 100000;
const int MAXM = 100000;
const int MAXLEN = MAXN * 2;
const int MAXLOG = 18;
const int INF = 0x3f3f3f3f;
template <class T>
void Read(T &x) {
  x = 0;
  char c = getchar();
  bool flag = 0;
  while (c < '0' || '9' < c) {
    if (c == '-') flag = 1;
    c = getchar();
  }
  while ('0' <= c && c <= '9') {
    x = x * 10 + c - '0';
    c = getchar();
  }
  if (flag) x = -x;
}
int n, m;
int A[MAXN + 10];
int cf[MAXLEN + 10], t[MAXLEN + 10];
namespace SA {
int sa[MAXLEN + 10];
int sum[MAXLEN + 10], wv[MAXLEN + 10];
int wa[MAXLEN + 10], wb[MAXLEN + 10];
int ranks[MAXLEN + 10], height[MAXLEN + 10];
inline bool cmp(int *x, int p1, int p2, int len) {
  return x[p1] == x[p2] && x[p1 + len] == x[p2 + len];
}
inline void getsa(int *cf, int n, int m) {
  int i, j, p = 0, *x = wa, *y = wb;
  for (i = 1; i <= m; ++i) sum[i] = 0;
  for (i = 1; i <= n; ++i) ++sum[x[i] = cf[i]];
  for (i = 1; i <= m; ++i) sum[i] += sum[i - 1];
  for (i = n; i >= 1; --i) sa[sum[x[i]]--] = i;
  for (j = 1, p = 1; p <= n; j <<= 1, m = p) {
    for (p = 0, i = n - j + 1; i <= n; ++i) y[++p] = i;
    for (i = 1; i <= n; ++i)
      if (sa[i] > j) y[++p] = sa[i] - j;
    for (i = 1; i <= n; ++i) wv[i] = x[y[i]];
    for (i = 1; i <= m; ++i) sum[i] = 0;
    for (i = 1; i <= n; ++i) ++sum[wv[i]];
    for (i = 1; i <= m; ++i) sum[i] += sum[i - 1];
    for (i = n; i >= 1; --i) sa[sum[wv[i]]--] = y[i];
    for (swap(x, y), p = 1, i = 1; i <= n; ++i)
      x[sa[i]] = cmp(y, sa[i - 1], sa[i], j) ? p : ++p;
  }
}
inline void getheight(int *cf, int n) {
  for (int i = 1; i <= n; ++i) ranks[sa[i]] = i;
  for (int i = 1, j, k = 0; i <= n; height[ranks[i++]] = k)
    for (k ? --k : 0, j = sa[ranks[i] - 1]; cf[i + k] == cf[j + k]; ++k)
      ;
}
}  // namespace SA
int l[MAXM + 10], r[MAXM + 10];
struct SegmentTree {
  int mx[MAXLEN * 4], mn[MAXLEN * 4];
  void build(int x, int l, int r) {
    if (l == r) {
      mx[x] = SA::height[l];
      mn[x] = SA::height[l];
      return;
    }
    int mid = (l + r) >> 1;
    build(x << 1, l, mid);
    build(x << 1 | 1, mid + 1, r);
    mx[x] = max(mx[x << 1], mx[x << 1 | 1]);
    mn[x] = min(mn[x << 1], mn[x << 1 | 1]);
  }
  int queryl(int x, int pos, int v, int ll, int rr) {
    if (rr <= pos && mx[x] < v) return rr + 1;
    if (rr <= pos && mn[x] >= v) return ll;
    int mid = (ll + rr) >> 1;
    if (pos <= mid)
      return queryl(x << 1, pos, v, ll, mid);
    else {
      int t = queryl(x << 1 | 1, pos, v, mid + 1, rr);
      if (t == mid + 1)
        return queryl(x << 1, pos, v, ll, mid);
      else
        return t;
    }
  }
  int queryr(int x, int pos, int v, int ll, int rr) {
    if (pos <= ll && mx[x] < v) return ll - 1;
    if (pos <= ll && mn[x] >= v) return rr;
    int mid = (ll + rr) >> 1;
    if (mid < pos)
      return queryr(x << 1 | 1, pos, v, mid + 1, rr);
    else {
      int t = queryr(x << 1, pos, v, ll, mid);
      if (t == mid)
        return queryr(x << 1 | 1, pos, v, mid + 1, rr);
      else
        return t;
    }
  }
} T;
struct SegTree {
  int sum[MAXLEN * MAXLOG * 2], cntp;
  int ls[MAXLEN * MAXLOG * 2], rs[MAXLEN * MAXLOG * 2];
  int rt[MAXLEN + 10];
  inline void init() { cntp = 0; }
  inline int newnode(int last) {
    int x = ++cntp;
    sum[x] = sum[last];
    ls[x] = ls[last], rs[x] = rs[last];
    return x;
  }
  void insert(int &x, int last, int pos, int ll, int rr) {
    x = newnode(last);
    ++sum[x];
    if (ll == rr)
      return;
    else {
      int mid = (ll + rr) >> 1;
      if (pos <= mid)
        insert(ls[x], ls[last], pos, ll, mid);
      else
        insert(rs[x], rs[last], pos, mid + 1, rr);
    }
  }
  int query(int x, int l, int r, int ll, int rr) {
    if (!x) return 0;
    if (l <= ll && rr <= r) return sum[x];
    int mid = (ll + rr) >> 1;
    int ret = 0;
    if (l <= mid) ret += query(ls[x], l, r, ll, mid);
    if (mid < r) ret += query(rs[x], l, r, mid + 1, rr);
    return ret;
  }
} T1, T2;
int main() {
  Read(n);
  for (int i = 1; i <= n; ++i) Read(A[i]);
  Read(m);
  for (int i = 1; i <= m; ++i) Read(l[i]), Read(r[i]);
  int len = n - 1, cnt;
  for (int i = 1; i <= len; ++i) cf[i] = A[i + 1] - A[i];
  cf[len + 1] = INF;
  for (int i = 1; i <= len; ++i) cf[len + 1 + i] = -cf[i];
  len = len + 1 + len;
  for (int i = 1; i <= len; ++i) t[i] = cf[i];
  sort(t + 1, t + len + 1);
  cnt = unique(t + 1, t + len + 1) - t - 1;
  for (int i = 1; i <= len; ++i)
    cf[i] = lower_bound(t + 1, t + cnt + 1, cf[i]) - t;
  SA::getsa(cf, len, cnt);
  SA::getheight(cf, len);
  T1.init(), T2.init();
  T.build(1, 1, len);
  for (int i = 1; i <= n - 1; ++i)
    T1.insert(T1.rt[i], T1.rt[i - 1], SA::ranks[i], 1, len);
  for (int i = n; i <= len; ++i) T1.rt[i] = T1.rt[i - 1];
  for (int i = n - 1; i >= 1; --i)
    T2.insert(T2.rt[i], T2.rt[i + 1], SA::ranks[i], 1, len);
  for (int i = 1; i <= m; ++i) {
    --r[i];
    int t = r[i] - l[i] + 1;
    int sufp = n + l[i];
    if (t == 0)
      printf("%d\n", n - 1);
    else {
      int ans = 0;
      int tl = T.queryl(1, SA::ranks[sufp], t, 1, len) - 1;
      int tr = SA::ranks[sufp] < len
                   ? T.queryr(1, SA::ranks[sufp] + 1, t, 1, len)
                   : len;
      if (tl > tr)
        puts("0");
      else {
        if (l[i] - t - 1 >= 1)
          ans += T1.query(T1.rt[l[i] - t - 1], tl, tr, 1, len);
        if (r[i] + t + 1 <= len)
          ans += T2.query(T2.rt[r[i] + 2], tl, tr, 1, len);
        printf("%d\n", ans);
      }
    }
  }
}