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 long long N = 2e5 + 10, LOG = 20, INF = 1e12;
long long a[N], s[N], RANK[LOG][N];
int lg[N], suf[N], rmq[LOG][N], rg[N], f[N];
pair<pair<long long, long long>, int> tmp[N];
int seg[LOG][4 * N];
int lcp(int i, int j) {
  int len = 0;
  for (int k = LOG - 1; k >= 0; k--) {
    if (RANK[k][i] == RANK[k][j]) {
      i += (1 << k);
      j += (1 << k);
      len += (1 << k);
    }
  }
  return len;
}
void suffix_array(int n) {
  for (int i = 0; i < n; i++) RANK[0][i] = s[i] + INF;
  for (int j = 1; j < LOG; j++) {
    for (int i = 0; i < n; i++) {
      if (i + (1 << (j - 1)) < n)
        tmp[i] = {{RANK[j - 1][i], RANK[j - 1][i + (1 << (j - 1))]}, i};
      else
        tmp[i] = {{RANK[j - 1][i], -1}, i};
    }
    sort(tmp, tmp + n);
    for (int i = 0; i < n; i++) {
      int id = tmp[i].second;
      if (i == 0)
        RANK[j][id] = 1;
      else if (tmp[i].first == tmp[i - 1].first)
        RANK[j][id] = RANK[j][tmp[i - 1].second];
      else
        RANK[j][id] = RANK[j][tmp[i - 1].second] + 1;
    }
  }
  for (int i = 0; i < n; i++) {
    suf[i] = tmp[i].second;
    f[tmp[i].second] = i;
  }
  for (int i = 0; i < n - 1; i++) rmq[0][i] = lcp(suf[i], suf[i + 1]);
  for (int j = 1; j < LOG; j++)
    for (int i = 0; i < n - 1; i++)
      if (i + (1 << j) <= n - 1)
        rmq[j][i] = min(rmq[j - 1][i], rmq[j - 1][i + (1 << (j - 1))]);
  return;
}
int get(int l, int r) {
  int len = r - l;
  return min(rmq[lg[len]][l], rmq[lg[len]][r - (1 << lg[len])]);
}
int solve(int tb, int L, int R, int xl, int xr, int s, int e) {
  if (xl > xr) return 0;
  if (L <= s && e <= R) {
    int up = upper_bound(seg[tb] + s, seg[tb] + e, xr) - (seg[tb] + s);
    int down = lower_bound(seg[tb] + s, seg[tb] + e, xl) - (seg[tb] + s);
    return up - down;
  }
  if (L >= e || s >= R) return 0;
  int mid = (s + e) / 2;
  return solve(tb + 1, L, R, xl, xr, s, mid) +
         solve(tb + 1, L, R, xl, xr, mid, e);
}
void build(int tb, int s, int e) {
  if (e - s < 2) {
    seg[tb][s] = suf[s];
    return;
  }
  int mid = (s + e) / 2;
  build(tb + 1, s, mid);
  build(tb + 1, mid, e);
  merge(seg[tb + 1] + s, seg[tb + 1] + mid, seg[tb + 1] + mid, seg[tb + 1] + e,
        seg[tb] + s);
}
int find(int l, int r, int n) {
  int lo, hi;
  lo = -1, hi = f[l];
  while (hi - lo > 1) {
    int mid = (lo + hi) / 2;
    if (get(mid, f[l]) >= r - l)
      hi = mid;
    else
      lo = mid;
  }
  int left = hi;
  lo = f[l], hi = n;
  while (hi - lo > 1) {
    int mid = (lo + hi) / 2;
    if (get(f[l], mid) >= r - l)
      lo = mid;
    else
      hi = mid;
  }
  int right = hi;
  int nn = (n + 1) / 2;
  l -= nn;
  r -= nn;
  return solve(0, left, right, 0, l - (r - l) - 1, 0, n) +
         solve(0, left, right, r + 1, nn - 2, 0, n);
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) cin >> a[i];
  for (int i = 0; i < n - 1; i++) s[i] = a[i + 1] - a[i];
  s[n - 1] = INF;
  for (int i = n; i < 2 * n - 1; i++) s[i] = a[i - n] - a[i - n + 1];
  for (int i = 2; i < N; i++) lg[i] = lg[i / 2] + 1;
  suffix_array(2 * n - 1);
  for (int i = n - 1; i >= 0; i--) {
    if (i == n - 1)
      rg[i] = n;
    else if (a[i] == a[i + 1])
      rg[i] = rg[i + 1];
    else
      rg[i] = i + 1;
  }
  build(0, 0, 2 * n - 1);
  int q;
  cin >> q;
  while (q--) {
    int l, r;
    cin >> l >> r;
    --l;
    if (r - l == 1) {
      cout << n - 1 << '\n';
    } else {
      r--;
      int res = find(l + n, r + n, 2 * n - 1);
      cout << res << '\n';
    }
  }
}