#include <bits/stdc++.h>
using namespace std;
template <typename T1, typename T2>
istream &operator>>(istream &in, pair<T1, T2> &a) {
  in >> a.first >> a.second;
  return in;
}
template <typename T1, typename T2>
ostream &operator<<(ostream &out, pair<T1, T2> a) {
  out << a.first << " " << a.second;
  return out;
}
template <typename T, typename T1>
T maxs(T &a, T1 b) {
  if (b > a) a = b;
  return a;
}
template <typename T, typename T1>
T mins(T &a, T1 b) {
  if (b < a) a = b;
  return a;
}
const long long N = 2e5 + 5;
struct node {
  long long a = 0;
  long long cnt = 0;
  long long ans = 0;
  long long ti = 0;
  long long lazy = 0;
  node(long long val = 0) { a = val; }
  void merge(node &n1, node &n2) {
    this->a = min(n1.a, n2.a);
    cnt = 0;
    if (this->a == n1.a) {
      cnt = n1.cnt;
    }
    if (this->a == n2.a) {
      cnt += n2.cnt;
    }
  }
};
template <typename node>
struct segtree {
  node t[4 * N];
  long long tl[4 * N];
  long long tr[4 * N];
  void push(long long v) {
    if (t[v].lazy) {
      if (tl[v] != tr[v]) {
        t[2 * v].lazy += t[v].lazy;
        t[2 * v + 1].lazy += t[v].lazy;
      }
      t[v].a += t[v].lazy;
      t[v].lazy = 0;
    }
    if (t[v].ti) {
      if (tl[v] != tr[v]) {
        long long val_l = t[2 * v].a + t[2 * v].lazy;
        long long val_r = t[2 * v + 1].a + t[2 * v + 1].lazy;
        if (val_l <= val_r) {
          t[2 * v].ti += t[v].ti;
        }
        if (val_r <= val_l) {
          t[2 * v + 1].ti += t[v].ti;
        }
      }
      t[v].ans += t[v].cnt * t[v].ti;
      t[v].ti = 0;
    }
  }
  void build(long long v, long long start, long long end) {
    tl[v] = start;
    tr[v] = end;
    if (start == end) {
      t[v].a = 1;
      t[v].cnt = 1;
    } else {
      long long m = (start + end) / 2;
      build(2 * v, start, m);
      build(2 * v + 1, m + 1, end);
      t[v].merge(t[2 * v], t[2 * v + 1]);
    }
  }
  void zeno(long long v, long long l, long long r, long long val) {
    push(v);
    if (tr[v] < l || tl[v] > r) return;
    if (l <= tl[v] && tr[v] <= r) {
      t[v].lazy = val;
      push(v);
      return;
    }
    zeno(2 * v, l, r, val);
    zeno(2 * v + 1, l, r, val);
    t[v].merge(t[2 * v], t[2 * v + 1]);
  }
  long long query(long long v, long long l, long long r) {
    if (tr[v] < l || tl[v] > r) return 0;
    push(v);
    if (l <= tl[v] && tr[v] <= r) {
      return t[v].ans;
    }
    long long a = query(2 * v, l, r);
    long long b = query(2 * v + 1, l, r);
    return a + b;
  }

 public:
  long long query(long long l, long long r) { return query(1, l, r); }
  void upd(long long l, long long r, long long val) {
    return zeno(1, l, r, val);
  }
};
segtree<node> seg;
vector<pair<long long, long long> > queries[N];
long long solve() {
  long long n;
  cin >> n;
  vector<long long> a(n + 1);
  for (long long i = 1; i < n + 1; i++) {
    cin >> a[i];
  }
  long long q;
  cin >> q;
  vector<long long> ans(q);
  for (long long i = 0; i < q; i++) {
    long long l, r;
    cin >> l >> r;
    queries[r].push_back({l, i});
  }
  seg.build(1, 1, n);
  vector<long long> mx = {0};
  vector<long long> mn = {0};
  for (long long i = 1; i <= n; i++) {
    while (mx.back() and a[i] > a[mx.back()]) {
      long long j = mx.back();
      mx.pop_back();
      long long delta = a[i] - a[j];
      seg.upd(mx.back() + 1, j, delta);
    }
    mx.push_back(i);
    while (mn.back() and a[i] < a[mn.back()]) {
      long long j = mn.back();
      mn.pop_back();
      long long delta = a[j] - a[i];
      seg.upd(mn.back() + 1, j, delta);
    }
    mn.push_back(i);
    seg.upd(1, i, -1);
    seg.t[1].ti++;
    for (auto j : queries[i]) {
      ans[j.second] = seg.query(j.first, i);
    }
  }
  for (auto i : ans) {
    cout << i << "\n";
  }
  return 0;
}
signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  long long t = 1;
  while (t--) {
    solve();
  }
  return 0;
}

### Prompt

Please create a solution in Cpp to the following problem:

A permutation p of length n is a sequence p_1, p_2, …, p_n consisting of n distinct integers, each of which from 1 to n (1 ≤ p_i ≤ n) .

Let's call the subsegment [l,r] of the permutation good if all numbers from the minimum on it to the maximum on this subsegment occur among the numbers p_l, p_{l+1}, ..., p_r.

For example, good segments of permutation [1, 3, 2, 5, 4] are:

  * [1, 1], 
  * [1, 3], 
  * [1, 5], 
  * [2, 2], 
  * [2, 3], 
  * [2, 5], 
  * [3, 3], 
  * [4, 4], 
  * [4, 5], 
  * [5, 5]. 



You are given a permutation p_1, p_2, …, p_n.

You need to answer q queries of the form: find the number of good subsegments of the given segment of permutation.

In other words, to answer one query, you need to calculate the number of good subsegments [x ... y] for some given segment [l ... r], such that l ≤ x ≤ y ≤ r.

Input

The first line contains a single integer n (1 ≤ n ≤ 120000) — the number of elements in the permutation.

The second line contains n distinct integers p_1, p_2, …, p_n separated by spaces (1 ≤ p_i ≤ n).

The third line contains an integer q (1 ≤ q ≤ 120000) — number of queries.

The following q lines describe queries, each line contains a pair of integers l, r separated by space (1 ≤ l ≤ r ≤ n).

Output

Print a q lines, i-th of them should contain a number of good subsegments of a segment, given in the i-th query.

Example

Input

5
1 3 2 5 4
15
1 1
1 2
1 3
1 4
1 5
2 2
2 3
2 4
2 5
3 3
3 4
3 5
4 4
4 5
5 5


Output

1
2
5
6
10
1
3
4
7
1
2
4
1
3
1

### Response

```cpp
#include <bits/stdc++.h>
using namespace std;
template <typename T1, typename T2>
istream &operator>>(istream &in, pair<T1, T2> &a) {
  in >> a.first >> a.second;
  return in;
}
template <typename T1, typename T2>
ostream &operator<<(ostream &out, pair<T1, T2> a) {
  out << a.first << " " << a.second;
  return out;
}
template <typename T, typename T1>
T maxs(T &a, T1 b) {
  if (b > a) a = b;
  return a;
}
template <typename T, typename T1>
T mins(T &a, T1 b) {
  if (b < a) a = b;
  return a;
}
const long long N = 2e5 + 5;
struct node {
  long long a = 0;
  long long cnt = 0;
  long long ans = 0;
  long long ti = 0;
  long long lazy = 0;
  node(long long val = 0) { a = val; }
  void merge(node &n1, node &n2) {
    this->a = min(n1.a, n2.a);
    cnt = 0;
    if (this->a == n1.a) {
      cnt = n1.cnt;
    }
    if (this->a == n2.a) {
      cnt += n2.cnt;
    }
  }
};
template <typename node>
struct segtree {
  node t[4 * N];
  long long tl[4 * N];
  long long tr[4 * N];
  void push(long long v) {
    if (t[v].lazy) {
      if (tl[v] != tr[v]) {
        t[2 * v].lazy += t[v].lazy;
        t[2 * v + 1].lazy += t[v].lazy;
      }
      t[v].a += t[v].lazy;
      t[v].lazy = 0;
    }
    if (t[v].ti) {
      if (tl[v] != tr[v]) {
        long long val_l = t[2 * v].a + t[2 * v].lazy;
        long long val_r = t[2 * v + 1].a + t[2 * v + 1].lazy;
        if (val_l <= val_r) {
          t[2 * v].ti += t[v].ti;
        }
        if (val_r <= val_l) {
          t[2 * v + 1].ti += t[v].ti;
        }
      }
      t[v].ans += t[v].cnt * t[v].ti;
      t[v].ti = 0;
    }
  }
  void build(long long v, long long start, long long end) {
    tl[v] = start;
    tr[v] = end;
    if (start == end) {
      t[v].a = 1;
      t[v].cnt = 1;
    } else {
      long long m = (start + end) / 2;
      build(2 * v, start, m);
      build(2 * v + 1, m + 1, end);
      t[v].merge(t[2 * v], t[2 * v + 1]);
    }
  }
  void zeno(long long v, long long l, long long r, long long val) {
    push(v);
    if (tr[v] < l || tl[v] > r) return;
    if (l <= tl[v] && tr[v] <= r) {
      t[v].lazy = val;
      push(v);
      return;
    }
    zeno(2 * v, l, r, val);
    zeno(2 * v + 1, l, r, val);
    t[v].merge(t[2 * v], t[2 * v + 1]);
  }
  long long query(long long v, long long l, long long r) {
    if (tr[v] < l || tl[v] > r) return 0;
    push(v);
    if (l <= tl[v] && tr[v] <= r) {
      return t[v].ans;
    }
    long long a = query(2 * v, l, r);
    long long b = query(2 * v + 1, l, r);
    return a + b;
  }

 public:
  long long query(long long l, long long r) { return query(1, l, r); }
  void upd(long long l, long long r, long long val) {
    return zeno(1, l, r, val);
  }
};
segtree<node> seg;
vector<pair<long long, long long> > queries[N];
long long solve() {
  long long n;
  cin >> n;
  vector<long long> a(n + 1);
  for (long long i = 1; i < n + 1; i++) {
    cin >> a[i];
  }
  long long q;
  cin >> q;
  vector<long long> ans(q);
  for (long long i = 0; i < q; i++) {
    long long l, r;
    cin >> l >> r;
    queries[r].push_back({l, i});
  }
  seg.build(1, 1, n);
  vector<long long> mx = {0};
  vector<long long> mn = {0};
  for (long long i = 1; i <= n; i++) {
    while (mx.back() and a[i] > a[mx.back()]) {
      long long j = mx.back();
      mx.pop_back();
      long long delta = a[i] - a[j];
      seg.upd(mx.back() + 1, j, delta);
    }
    mx.push_back(i);
    while (mn.back() and a[i] < a[mn.back()]) {
      long long j = mn.back();
      mn.pop_back();
      long long delta = a[j] - a[i];
      seg.upd(mn.back() + 1, j, delta);
    }
    mn.push_back(i);
    seg.upd(1, i, -1);
    seg.t[1].ti++;
    for (auto j : queries[i]) {
      ans[j.second] = seg.query(j.first, i);
    }
  }
  for (auto i : ans) {
    cout << i << "\n";
  }
  return 0;
}
signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  long long t = 1;
  while (t--) {
    solve();
  }
  return 0;
}

```