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;
template <typename T>
void chkmax(T &x, T y) {
  x = x > y ? x : y;
}
template <typename T>
void chkmin(T &x, T y) {
  x = x > y ? y : x;
}
const long long INF = (1ll << 62) - 1;
template <typename T>
void read(T &x) {
  x = 0;
  bool f = 1;
  char ch;
  do {
    ch = getchar();
    if (ch == '-') f = 0;
  } while (ch > '9' || ch < '0');
  do {
    x = x * 10 + ch - '0';
    ch = getchar();
  } while (ch >= '0' && ch <= '9');
  x = f ? x : -x;
}
template <typename T>
void write(T x) {
  if (x < 0) x = ~x + 1, putchar('-');
  if (x > 9) write(x / 10);
  putchar(x % 10 + '0');
}
const int N = 2e5 + 7;
const int K = 20 + 5;
const int NLOGN = 8e6 + 7;
struct EDGE {
  int to, nxt;
} edge[N << 1];
int n, q, E, a[N], head[N];
inline void addedge(int u, int v) {
  edge[++E].to = v;
  edge[E].nxt = head[u];
  head[u] = E;
}
struct Segment_Tree {
  int cnt, rt[N];
  struct segment_tree {
    int ls, rs, cnt;
  } t[NLOGN];
  inline void insert(int &p, int l, int r, int v) {
    if (!p) p = ++cnt;
    if (l == r) {
      t[p].cnt++;
      return;
    }
    int mid = (l + r) >> 1;
    if (v <= mid)
      insert((t[p].ls), l, mid, v);
    else
      insert((t[p].rs), mid + 1, r, v);
    t[p].cnt = t[(t[p].ls)].cnt + t[(t[p].rs)].cnt;
  }
  inline int merge(int x, int y, int l, int r) {
    if (!x || !y) return x + y;
    int p = ++cnt;
    if (l == r) {
      t[p].cnt = t[x].cnt + t[y].cnt;
      return p;
    }
    int mid = (l + r) >> 1;
    (t[p].ls) = merge((t[x].ls), (t[y].ls), l, mid);
    (t[p].rs) = merge((t[x].rs), (t[y].rs), mid + 1, r);
    t[p].cnt = t[(t[p].ls)].cnt + t[(t[p].rs)].cnt;
    return p;
  }
  inline int query(int p, int l, int r, int L, int R) {
    if (!p) return 0;
    if (L <= l && r <= R) return t[p].cnt;
    int mid = (l + r) >> 1, ans = 0;
    if (L <= mid) ans += query((t[p].ls), l, mid, L, R);
    if (R > mid) ans += query((t[p].rs), mid + 1, r, L, R);
    return ans;
  }
} SGT;
struct SAM {
  int lst, cnt, ed[N], fa[N], mxl[N], len[N], f[N][K];
  map<int, int> ch[N];
  SAM() { lst = cnt = 1; }
  inline void expend(int c) {
    int p = lst, q = ++cnt;
    lst = q;
    len[q] = len[p] + 1;
    while (p && !ch[p][c]) {
      ch[p][c] = q;
      p = fa[p];
    }
    if (p == 0) {
      fa[q] = 1;
    } else {
      int x = ch[p][c];
      if (len[x] == len[p] + 1) {
        fa[q] = x;
      } else {
        int y = ++cnt;
        fa[y] = fa[x];
        len[y] = len[p] + 1;
        fa[x] = fa[q] = y;
        ch[y] = ch[x];
        while (p && ch[p][c] == x) {
          ch[p][c] = y;
          p = fa[p];
        }
      }
    }
  }
  inline void dfs(int u) {
    for (int i = 1; i <= 20; i++) f[u][i] = f[f[u][i - 1]][i - 1];
    for (int i = head[u]; i; i = edge[i].nxt) {
      int v = edge[i].to;
      f[v][0] = u;
      dfs(v);
      SGT.rt[u] = SGT.merge(SGT.rt[u], SGT.rt[v], 1, n - 1);
    }
  }
  inline void build() {
    for (int i = 2; i <= cnt; i++) addedge(fa[i], i);
    dfs(1);
    int p = 1, ml = 0;
    for (int i = 1; i < n; i++) {
      if (ch[p][-a[i]])
        p = ch[p][-a[i]], ml++;
      else {
        while (p && !ch[p][-a[i]]) p = fa[p];
        if (!p)
          p = 1, ml = 0;
        else
          ml = len[p] + 1, p = ch[p][-a[i]];
      }
      ed[i] = p;
      mxl[i] = ml;
    }
  }
} SAM;
inline void query(int l, int r) {
  if (l == r) return (void)printf("%d\n", n - 1);
  int p = SAM.ed[r - 1];
  if (SAM.mxl[r - 1] < r - l) return (void)puts("0");
  for (int i = 20; i >= 0; i--)
    if (SAM.len[SAM.f[p][i]] >= r - l) p = SAM.f[p][i];
  printf("%d\n", SGT.query(SGT.rt[p], 1, n - 1, 1, l - 2) +
                     SGT.query(SGT.rt[p], 1, n - 1, r * 2 - l, n - 1));
  return;
}
int main() {
  read(n);
  for (int i = 1; i <= n; i++) read(a[i]);
  for (int i = 1; i < n; i++) a[i] = a[i + 1] - a[i];
  for (int i = 1; i < n; i++) {
    SAM.expend(a[i]);
    SGT.insert(SGT.rt[SAM.lst], 1, n - 1, i);
  }
  SAM.build();
  read(q);
  for (int i = 1, l, r; i <= q; i++) {
    read(l);
    read(r);
    query(l, r);
  }
  return 0;
}