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 N = 4e5 + 10;
int n, cnt;
vector<int> to[N];
int v[N], pos[N], dep[N], f[N][23], dfn[N][2], ans[N];
struct SAM {
  int tot, tail;
  map<int, int> son[N];
  int fa[N], maxlen[N];
  SAM() { tot = tail = 1; }
  inline void copy(int p, int q) { fa[p] = fa[q], son[p] = son[q]; }
  inline void ins(int ch) {
    int p = tail, now = tail = ++tot;
    maxlen[now] = maxlen[p] + 1;
    for (; p && son[p].find(ch) == son[p].end(); p = fa[p]) son[p][ch] = now;
    if (!p) {
      fa[now] = 1;
      return;
    }
    int cur1 = son[p][ch], cur2;
    if (maxlen[cur1] == maxlen[p] + 1) {
      fa[now] = cur1;
      return;
    }
    copy(cur2 = ++tot, cur1);
    maxlen[cur2] = maxlen[p] + 1;
    for (; p && son[p][ch] == cur1; p = fa[p]) son[p][ch] = cur2;
    fa[now] = fa[cur1] = cur2;
  }
} t;
inline void dfs(int p) {
  dfn[p][0] = ++cnt;
  for (int q : to[p]) {
    dep[q] = dep[p] + 1, f[q][0] = p;
    for (int j = 1; j <= log2(dep[q]); j++) f[q][j] = f[f[q][j - 1]][j - 1];
    dfs(q);
  }
  dfn[p][1] = cnt;
}
inline int jump(int x, int lim) {
  for (int i = log2(dep[x]); i >= 0; i--)
    if (t.maxlen[f[x][i]] >= lim) x = f[x][i];
  return x;
}
struct ask {
  int type, x, y;
} que[N * 6];
inline bool cmp(ask a, ask b) {
  if (a.x != b.x) return a.x < b.x;
  if (a.y != b.y) return a.y < b.y;
  return abs(a.type) < abs(b.type);
}
struct tree {
  int val[N];
  inline int lowbit(int x) { return x & (-x); }
  inline void change(int x) {
    for (int i = x; i <= n; i += lowbit(i)) val[i]++;
  }
  inline int query(int x) {
    int res = 0;
    for (int i = x; i >= 1; i -= lowbit(i)) res += val[i];
    return res;
  }
} rt;
int main() {
  int T, a, b, idx = 0;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) scanf("%d", &v[i]);
  for (int i = n; i >= 1; i--) v[i] -= v[i - 1];
  for (int i = 1; i <= n; i++) v[i + n] = -v[i];
  for (int i = 1; i <= 2 * n; i++) t.ins(v[i]), pos[i] = t.tail;
  for (int i = 1; i <= t.tot; i++) to[t.fa[i]].push_back(i);
  dep[1] = 1, dfs(1);
  scanf("%d", &T);
  t.maxlen[0] = -1;
  for (int i = 1; i <= T; i++) {
    scanf("%d%d", &a, &b);
    if (a == b) {
      ans[i] = n - 1;
      continue;
    }
    int len = b - a + 1, pla = jump(pos[b + n], len - 1);
    if (b + len <= n)
      que[++idx] = (ask){i, dfn[pla][1], n},
      que[++idx] = (ask){-i, dfn[pla][1], b + len - 1};
    if (a >= 2) que[++idx] = (ask){i, dfn[pla][1], a - 1};
    if (b + len <= n)
      que[++idx] = (ask){-i, dfn[pla][0] - 1, n},
      que[++idx] = (ask){i, dfn[pla][0] - 1, b + len - 1};
    if (a >= 2) que[++idx] = (ask){-i, dfn[pla][0] - 1, a - 1};
  }
  for (int i = 2; i <= n; i++) que[++idx] = (ask){0, dfn[pos[i]][0], i};
  sort(que + 1, que + idx + 1, cmp);
  for (int i = 1; i <= idx; i++) {
    if (que[i].type == 0) rt.change(que[i].y);
    if (que[i].type > 0) ans[que[i].type] += rt.query(que[i].y);
    if (que[i].type < 0) ans[-que[i].type] -= rt.query(que[i].y);
  }
  for (int i = 1; i <= T; i++) printf("%d\n", ans[i]);
  return 0;
}