#include <bits/stdc++.h>
using namespace std;
template <typename T>
void read(T &x) {
  x = 0;
  bool f = 0;
  char c = getchar();
  for (; !isdigit(c); c = getchar())
    if (c == '-') f = 1;
  for (; isdigit(c); c = getchar()) x = x * 10 + (c ^ 48);
  if (f) x = -x;
}
template <typename F>
inline void write(F x, char ed = '\n') {
  static short st[30];
  short tp = 0;
  if (x < 0) putchar('-'), x = -x;
  do st[++tp] = x % 10, x /= 10;
  while (x);
  while (tp) putchar('0' | st[tp--]);
  putchar(ed);
}
template <typename T>
inline void Mx(T &x, T y) {
  x < y && (x = y);
}
template <typename T>
inline void Mn(T &x, T y) {
  x > y && (x = y);
}
const int N = 200500;
struct node {
  int mn, add;
  long long res, cnt, cc;
} t[N << 3];
int p[N], s1[N], s2[N], t1, t2, q, n;
inline void Add(int p, int val) { t[p].add += val, t[p].mn += val; }
inline void Ans(int p, int c, int mn) {
  if (t[p].mn != mn) return;
  t[p].res += t[p].cnt * c, t[p].cc += c;
}
void spread(int p) {
  if (t[p].add) Add(p << 1, t[p].add), Add(p << 1 | 1, t[p].add), t[p].add = 0;
  if (t[p].cc)
    Ans(p << 1, t[p].cc, t[p].mn), Ans(p << 1 | 1, t[p].cc, t[p].mn),
        t[p].cc = 0;
}
void build(int p, int l, int r) {
  t[p].mn = 1e9, t[p].cnt = 1;
  if (l == r) return;
  int mid = (l + r) >> 1;
  build(p << 1, l, mid), build(p << 1 | 1, mid + 1, r);
  t[p].cnt = t[p << 1].cnt + t[p << 1 | 1].cnt;
}
void Update(int p) {
  if (t[p << 1].mn < t[p << 1 | 1].mn)
    t[p].mn = t[p << 1].mn, t[p].cnt = t[p << 1].cnt;
  else if (t[p << 1].mn > t[p << 1 | 1].mn)
    t[p].mn = t[p << 1 | 1].mn, t[p].cnt = t[p << 1 | 1].cnt;
  else
    t[p].mn = t[p << 1].mn, t[p].cnt = t[p << 1].cnt + t[p << 1 | 1].cnt;
}
void update(int p, int l, int r, int L, int R, int val) {
  if (L <= l && r <= R) return Add(p, val);
  spread(p);
  int mid = (l + r) >> 1;
  if (L <= mid) update(p << 1, l, mid, L, R, val);
  if (mid < R) update(p << 1 | 1, mid + 1, r, L, R, val);
  Update(p);
}
void refresh(int p, int l, int r, int x) {
  if (l == r) return t[p].mn = 0, void();
  int mid = (l + r) >> 1;
  spread(p);
  if (x <= mid)
    refresh(p << 1, l, mid, x);
  else
    refresh(p << 1 | 1, mid + 1, r, x);
  Update(p);
}
void maintain(int p, int l, int r, int L, int R) {
  if (L <= l && r <= R) return Ans(p, 1, 0);
  int mid = (l + r) >> 1;
  spread(p);
  if (L <= mid) maintain(p << 1, l, mid, L, R);
  if (mid < R) maintain(p << 1 | 1, mid + 1, r, L, R);
  t[p].res = t[p << 1].res + t[p << 1 | 1].res;
}
long long query(int p, int l, int r, int L, int R) {
  if (L <= l && r <= R) return t[p].res;
  int mid = (l + r) >> 1;
  spread(p);
  long long ans = 0;
  if (L <= mid) ans += query(p << 1, l, mid, L, R);
  if (mid < R) ans += query(p << 1 | 1, mid + 1, r, L, R);
  return ans;
}
vector<pair<int, int> > v[N];
long long ans[N];
int main() {
  read(n);
  for (int i = 1; i <= n; i++) read(p[i]);
  read(q);
  for (int i = 1, l, r; i <= q; i++) read(l), read(r), v[r].emplace_back(l, i);
  build(1, 1, n);
  for (int i = 1; i <= n; i++) {
    while (t1 > 0 && p[s1[t1]] < p[i])
      update(1, 1, n, s1[t1 - 1] + 1, s1[t1], -p[s1[t1]]), t1--;
    while (t2 > 0 && p[s2[t2]] > p[i])
      update(1, 1, n, s2[t2 - 1] + 1, s2[t2], p[s2[t2]]), t2--;
    if (i != 1) update(1, 1, n, 1, i - 1, -1);
    refresh(1, 1, n, i);
    s1[++t1] = i, s2[++t2] = i;
    update(1, 1, n, s1[t1 - 1] + 1, i, p[i]);
    update(1, 1, n, s2[t2 - 1] + 1, i, -p[i]);
    Ans(1, 1, 0);
    for (auto t : v[i]) ans[t.second] = query(1, 1, n, t.first, i);
  }
  for (int i = 1; i <= q; i++) write(ans[i]);
  return 0;
}

### Prompt

Please formulate a Cpp solution 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 T>
void read(T &x) {
  x = 0;
  bool f = 0;
  char c = getchar();
  for (; !isdigit(c); c = getchar())
    if (c == '-') f = 1;
  for (; isdigit(c); c = getchar()) x = x * 10 + (c ^ 48);
  if (f) x = -x;
}
template <typename F>
inline void write(F x, char ed = '\n') {
  static short st[30];
  short tp = 0;
  if (x < 0) putchar('-'), x = -x;
  do st[++tp] = x % 10, x /= 10;
  while (x);
  while (tp) putchar('0' | st[tp--]);
  putchar(ed);
}
template <typename T>
inline void Mx(T &x, T y) {
  x < y && (x = y);
}
template <typename T>
inline void Mn(T &x, T y) {
  x > y && (x = y);
}
const int N = 200500;
struct node {
  int mn, add;
  long long res, cnt, cc;
} t[N << 3];
int p[N], s1[N], s2[N], t1, t2, q, n;
inline void Add(int p, int val) { t[p].add += val, t[p].mn += val; }
inline void Ans(int p, int c, int mn) {
  if (t[p].mn != mn) return;
  t[p].res += t[p].cnt * c, t[p].cc += c;
}
void spread(int p) {
  if (t[p].add) Add(p << 1, t[p].add), Add(p << 1 | 1, t[p].add), t[p].add = 0;
  if (t[p].cc)
    Ans(p << 1, t[p].cc, t[p].mn), Ans(p << 1 | 1, t[p].cc, t[p].mn),
        t[p].cc = 0;
}
void build(int p, int l, int r) {
  t[p].mn = 1e9, t[p].cnt = 1;
  if (l == r) return;
  int mid = (l + r) >> 1;
  build(p << 1, l, mid), build(p << 1 | 1, mid + 1, r);
  t[p].cnt = t[p << 1].cnt + t[p << 1 | 1].cnt;
}
void Update(int p) {
  if (t[p << 1].mn < t[p << 1 | 1].mn)
    t[p].mn = t[p << 1].mn, t[p].cnt = t[p << 1].cnt;
  else if (t[p << 1].mn > t[p << 1 | 1].mn)
    t[p].mn = t[p << 1 | 1].mn, t[p].cnt = t[p << 1 | 1].cnt;
  else
    t[p].mn = t[p << 1].mn, t[p].cnt = t[p << 1].cnt + t[p << 1 | 1].cnt;
}
void update(int p, int l, int r, int L, int R, int val) {
  if (L <= l && r <= R) return Add(p, val);
  spread(p);
  int mid = (l + r) >> 1;
  if (L <= mid) update(p << 1, l, mid, L, R, val);
  if (mid < R) update(p << 1 | 1, mid + 1, r, L, R, val);
  Update(p);
}
void refresh(int p, int l, int r, int x) {
  if (l == r) return t[p].mn = 0, void();
  int mid = (l + r) >> 1;
  spread(p);
  if (x <= mid)
    refresh(p << 1, l, mid, x);
  else
    refresh(p << 1 | 1, mid + 1, r, x);
  Update(p);
}
void maintain(int p, int l, int r, int L, int R) {
  if (L <= l && r <= R) return Ans(p, 1, 0);
  int mid = (l + r) >> 1;
  spread(p);
  if (L <= mid) maintain(p << 1, l, mid, L, R);
  if (mid < R) maintain(p << 1 | 1, mid + 1, r, L, R);
  t[p].res = t[p << 1].res + t[p << 1 | 1].res;
}
long long query(int p, int l, int r, int L, int R) {
  if (L <= l && r <= R) return t[p].res;
  int mid = (l + r) >> 1;
  spread(p);
  long long ans = 0;
  if (L <= mid) ans += query(p << 1, l, mid, L, R);
  if (mid < R) ans += query(p << 1 | 1, mid + 1, r, L, R);
  return ans;
}
vector<pair<int, int> > v[N];
long long ans[N];
int main() {
  read(n);
  for (int i = 1; i <= n; i++) read(p[i]);
  read(q);
  for (int i = 1, l, r; i <= q; i++) read(l), read(r), v[r].emplace_back(l, i);
  build(1, 1, n);
  for (int i = 1; i <= n; i++) {
    while (t1 > 0 && p[s1[t1]] < p[i])
      update(1, 1, n, s1[t1 - 1] + 1, s1[t1], -p[s1[t1]]), t1--;
    while (t2 > 0 && p[s2[t2]] > p[i])
      update(1, 1, n, s2[t2 - 1] + 1, s2[t2], p[s2[t2]]), t2--;
    if (i != 1) update(1, 1, n, 1, i - 1, -1);
    refresh(1, 1, n, i);
    s1[++t1] = i, s2[++t2] = i;
    update(1, 1, n, s1[t1 - 1] + 1, i, p[i]);
    update(1, 1, n, s2[t2 - 1] + 1, i, -p[i]);
    Ans(1, 1, 0);
    for (auto t : v[i]) ans[t.second] = query(1, 1, n, t.first, i);
  }
  for (int i = 1; i <= q; i++) write(ans[i]);
  return 0;
}

```