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>
#pragma GCC optimize("O2,unroll-loops,no-stack-protector,fast-math")
using namespace std;
const int inf = 1000000010;
const int MAXN = 200010, LOG = 19;
int n, m, k, u, v, x, y, t, a, b, ans;
int A[MAXN];
int Rank[MAXN][LOG], P[MAXN], PR[MAXN], pw, N;
int Lcp[MAXN][LOG];
int root[MAXN];
int seg[LOG * MAXN], L[MAXN * LOG], R[MAXN * LOG], ts;
bool cmp(int x, int y) {
  if (Rank[x][pw - 1] != Rank[y][pw - 1])
    return Rank[x][pw - 1] < Rank[y][pw - 1];
  if (max(x, y) + (1 << (pw - 1)) > N) return x > y;
  return Rank[x + (1 << (pw - 1))][pw - 1] < Rank[y + (1 << (pw - 1))][pw - 1];
}
void BuildSuffixArray() {
  for (int i = 1; i <= N; i++) Rank[i][0] = A[i], P[i] = i;
  for (pw = 1; pw < LOG; pw++) {
    sort(P + 1, P + N + 1, cmp);
    Rank[P[1]][pw] = 1;
    for (int i = 2; i <= N; i++)
      Rank[P[i]][pw] = Rank[P[i - 1]][pw] + cmp(P[i - 1], P[i]);
  }
}
int Add(int id, int tl, int tr, int pos) {
  if (pos < tl || tr <= pos) return id;
  int res = ++ts;
  if (tr - tl == 1)
    seg[res] = 1;
  else {
    int mid = (tl + tr) >> 1;
    L[res] = Add(L[id], tl, mid, pos);
    R[res] = Add(R[id], mid, tr, pos);
    seg[res] = seg[L[res]] + seg[R[res]];
  }
  return res;
}
int Get(int id, int tl, int tr, int l, int r) {
  if (r <= tl || tr <= l) return 0;
  if (l <= tl && tr <= r) return seg[id];
  int mid = (tl + tr) >> 1;
  return Get(L[id], tl, mid, l, r) + Get(R[id], mid, tr, l, r);
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> n;
  for (int i = 1; i <= n; i++) cin >> A[i];
  for (int i = 1; i < n; i++) A[i] -= A[i + 1];
  for (int i = 1; i < n; i++) A[i + n] = -A[i];
  A[n] = inf;
  N = 2 * n - 1;
  BuildSuffixArray();
  for (int i = 1; i <= N; i++) PR[P[i]] = i;
  for (int i = 1; i + 1 <= N; i++) {
    int x = P[i], y = P[i + 1];
    if (x < y) swap(x, y);
    for (int j = LOG - 1; ~j && x <= N; j--)
      if (Rank[x][j] == Rank[y][j]) {
        Lcp[i][0] |= (1 << j);
        x += (1 << j);
        y += (1 << j);
      }
  }
  for (int j = 1; j < LOG; j++)
    for (int i = 1; i + (1 << j) <= N + 1; i++)
      Lcp[i][j] = min(Lcp[i][j - 1], Lcp[i + (1 << (j - 1))][j - 1]);
  for (int i = 1; i <= N; i++) root[i] = Add(root[i - 1], 1, N + 1, PR[i]);
  cin >> m;
  while (m--) {
    cin >> x >> y;
    if (x == y) {
      cout << n - 1 << '\n';
      continue;
    }
    y -= x;
    x += n;
    int l = PR[x], r = PR[x];
    for (int i = LOG - 1; ~i; i--)
      if (l - (1 << i) > 0 && Lcp[l - (1 << i)][i] >= y) l -= (1 << i);
    for (int i = LOG - 1; ~i; i--)
      if (r + (1 << i) <= N && Lcp[r][i] >= y) r += (1 << i);
    r++;
    x -= n;
    ans = 0;
    if (0 < x - y + 1) ans += Get(root[x - y - 1], 1, N + 1, l, r);
    ans += Get(root[n], 1, N + 1, l, r);
    ans -= Get(root[x + y], 1, N + 1, l, r);
    cout << ans << '\n';
  }
  return 0;
}