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 <class T1, class T2, class T3 = hash<T1>>
using umap = unordered_map<T1, T2, T3>;
template <class T>
using uset = unordered_set<T>;
template <class T>
using vec = vector<T>;
const long long infll = numeric_limits<long long>::max() >> 1;
const int inf = numeric_limits<int>::max() >> 1;
const int N = 2e5 + 5;
const int L = 20;
int n, q, lgx;
int t[N];
int s[N];
int a[N];
int b[N];
int o[N];
int p[N][L];
int arr[N];
int acp[N];
int bcp[N];
int ocp[N];
int cnt[N];
int bit[N];
int ans[N];
int spt[N][L];
bool flag;
vec<int> sml;
vec<array<int, 4>> qry[N];
inline int lg(int x) { return 31 - __builtin_clz(x); }
void update(int i, int x) {
  for (; i < N; i += (i) & -(i)) {
    bit[i] += x;
  }
}
int query(int i) {
  int res = 0;
  for (; i; i -= (i) & -(i)) {
    res += bit[i];
  }
  return res;
}
int sum(int l, int r) { return query(r) - query(l - 1); }
void input() {
  cin >> n;
  for (int i = 1; i <= n; ++i) {
    cin >> arr[i];
  }
  n--;
  for (int i = 1; i <= n; ++i) {
    t[i] = arr[i + 1] - arr[i];
  }
  for (int i = n + 1; i <= 2 * n; ++i) {
    t[i] = -t[i - n];
  }
  n *= 2;
  flag |= arr[1] == 50;
}
void calc_spt() {
  for (int j = 1; j < L; ++j) {
    for (int i = 1; i + (1 << j) <= n; ++i) {
      spt[i][j] = min(spt[i][j - 1], spt[i + (1 << j - 1)][j - 1]);
    }
  }
}
int query(int l, int r) {
  if (l == r) {
    return n - s[l] + 1;
  }
  r--;
  int lgy = lg(r - l + 1);
  return min(spt[l][lgy], spt[r - (1 << lgy) + 1][lgy]);
}
int lcp(int i, int j) {
  int res = 0;
  for (int k = lgx; k >= 0 && i <= n && j <= n; --k) {
    if (p[i][k] == p[j][k]) {
      i += 1 << k;
      j += 1 << k;
      res += 1 << k;
    }
  }
  return res;
}
void compress() {
  for (int i = 1; i <= n; ++i) {
    sml.push_back(t[i]);
  }
  sort((sml).begin(), (sml).end());
  sml.erase(unique((sml).begin(), (sml).end()), sml.end());
  for (int i = 1; i <= n; ++i) {
    t[i] = lower_bound((sml).begin(), (sml).end(), t[i]) - sml.begin();
  }
}
void counting_sort(int a[], int b[], int o[]) {
  fill(cnt, cnt + N, 0);
  for (int i = 1; i <= n; ++i) {
    cnt[a[i]]++;
  }
  for (int i = 1; i < N; ++i) {
    cnt[i] += cnt[i - 1];
  }
  for (int i = n; i >= 1; --i) {
    int ind = cnt[a[i]]--;
    acp[ind] = a[i];
    bcp[ind] = b[i];
    ocp[ind] = o[i];
  }
  copy(acp, acp + N, a);
  copy(bcp, bcp + N, b);
  copy(ocp, ocp + N, o);
}
void radix_sort() {
  counting_sort(b, a, o);
  counting_sort(a, b, o);
}
int calc_lef(int l, int r, int p, int x) {
  if (l == r) {
    return l;
  }
  int q = l + r >> 1;
  if (query(q, p) >= x) {
    return calc_lef(l, q, p, x);
  } else {
    return calc_lef(q + 1, r, p, x);
  }
}
int calc_rig(int l, int r, int p, int x) {
  if (l == r) {
    return l;
  }
  int q = l + r + 1 >> 1;
  if (query(p, q) >= x) {
    return calc_rig(q, r, p, x);
  } else {
    return calc_rig(l, q - 1, p, x);
  }
}
void interval(int &lx, int &rx, int l, int d) {
  lx = calc_lef(1, l, l, d);
  rx = calc_rig(l, n, l, d);
}
void add_query(int lx, int rx, int l, int r, int i) {
  lx--;
  if (l > n || r <= 0 || l > r) {
    return;
  }
  qry[rx].push_back({l, r, 1, i});
  qry[lx].push_back({l, r, -1, i});
}
void sweep_line() {
  for (int i = 1; i <= n; ++i) {
    update(s[i], 1);
    for (auto it : qry[i]) {
      ans[it[3]] += it[2] * sum(it[0], it[1]);
    }
  }
}
void solve() {
  compress();
  for (int i = 1; i <= n; ++i) {
    p[i][0] = t[i];
  }
  for (int i = 1, j = 1; j <= n - 1; ++i, j <<= 1) {
    for (int k = 1; k <= n; ++k) {
      a[k] = p[k][i - 1] + 1;
      b[k] = (k + j <= n ? p[k + j][i - 1] : -1) + 1;
      o[k] = k;
    }
    radix_sort();
    p[o[1]][i] = 1;
    for (int k = 2; k <= n; ++k) {
      p[o[k]][i] = make_pair(a[k], b[k]) == make_pair(a[k - 1], b[k - 1])
                       ? p[o[k - 1]][i]
                       : k;
    }
    lgx = i;
  }
  for (int i = 1; i <= n; ++i) {
    s[p[i][lgx]] = i;
  }
  for (int i = 1; i < n; ++i) {
    spt[i][0] = lcp(s[i], s[i + 1]);
  }
  calc_spt();
  cin >> q;
  for (int i = 1; i <= q; ++i) {
    int l;
    cin >> l;
    int r;
    cin >> r;
    if (l == r) {
      ans[i] = n / 2;
      continue;
    }
    int lx;
    int rx;
    int d = r - l;
    interval(lx, rx, p[l + n / 2][lgx], d);
    add_query(lx, rx, r + 1, n / 2 - d + 1, i);
    add_query(lx, rx, 1, l - d - 1, i);
  }
  sweep_line();
  for (int i = 1; i <= q; ++i) {
    cout << ans[i] << "\n";
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0), cout.tie(0);
  input();
  solve();
}