#include <bits/stdc++.h>
using namespace std;
template <typename T>
inline bool upmin(T &x, T y) {
  return y < x ? x = y, 1 : 0;
}
template <typename T>
inline bool upmax(T &x, T y) {
  return x < y ? x = y, 1 : 0;
}
const long double eps = 1e-9;
const long double pi = acos(-1);
const int oo = 1 << 30;
const long long loo = 1ll << 62;
const int mods = 1e9 + 7;
const int MAXN = 300005;
const int INF = 0x3f3f3f3f;
namespace FastIO {
constexpr int SIZE = (1 << 21) + 1;
int num = 0, f;
char ibuf[SIZE], obuf[SIZE], que[65], *iS, *iT, *oS = obuf,
                                                *oT = obuf + SIZE - 1, c;
inline void flush() {
  fwrite(obuf, 1, oS - obuf, stdout);
  oS = obuf;
}
inline void putc(char c) {
  *oS++ = c;
  if (oS == oT) flush();
}
inline void getc(char &c) {
  for (c = (iS == iT ? (iT = ((iS = ibuf) + fread(ibuf, 1, SIZE, stdin)),
                       (iS == iT ? EOF : *iS++))
                     : *iS++);
       c < 'A' || c > 'Z';
       c = (iS == iT ? (iT = ((iS = ibuf) + fread(ibuf, 1, SIZE, stdin)),
                       (iS == iT ? EOF : *iS++))
                     : *iS++))
    ;
}
template <class I>
inline void read(I &x) {
  for (f = 1, c = (iS == iT ? (iT = ((iS = ibuf) + fread(ibuf, 1, SIZE, stdin)),
                              (iS == iT ? EOF : *iS++))
                            : *iS++);
       c < '0' || c > '9';
       c = (iS == iT ? (iT = ((iS = ibuf) + fread(ibuf, 1, SIZE, stdin)),
                       (iS == iT ? EOF : *iS++))
                     : *iS++))
    if (c == '-') f = -1;
  for (x = 0; c >= '0' && c <= '9';
       c = (iS == iT ? (iT = ((iS = ibuf) + fread(ibuf, 1, SIZE, stdin)),
                       (iS == iT ? EOF : *iS++))
                     : *iS++))
    x = (x << 3) + (x << 1) + (c & 15);
  x *= f;
}
template <class I>
inline void print(I x) {
  if (x < 0) putc('-'), x = -x;
  if (!x) putc('0');
  while (x) que[++num] = x % 10 + 48, x /= 10;
  while (num) putc(que[num--]);
}
struct Flusher_ {
  ~Flusher_() { flush(); }
} io_Flusher_;
}  // namespace FastIO
using FastIO ::print;
using FastIO ::putc;
using FastIO ::read;
long long Ans[MAXN], c[MAXN << 2], tagc[MAXN << 2];
int MX = 0, tag[MAXN << 2], mn[MAXN << 2], s[MAXN << 2], stkmx[MAXN],
    stkmn[MAXN], p[MAXN];
void up(int x) {
  mn[x] = min(mn[x << 1], mn[x << 1 | 1]);
  s[x] = (mn[x << 1] == mn[x]) * s[x << 1] +
         (mn[x << 1 | 1] == mn[x]) * s[x << 1 | 1];
}
void Down(int x) {
  if (tag[x] != 0) {
    mn[x << 1] += tag[x], mn[x << 1 | 1] += tag[x];
    tag[x << 1] += tag[x], tag[x << 1 | 1] += tag[x];
    tag[x] = 0;
  }
}
void down(int x) {
  Down(x);
  if (tagc[x] != 0) {
    if (mn[x << 1] == mn[x])
      c[x << 1] += tagc[x] * s[x << 1], tagc[x << 1] += tagc[x];
    if (mn[x << 1 | 1] == mn[x])
      c[x << 1 | 1] += tagc[x] * s[x << 1 | 1], tagc[x << 1 | 1] += tagc[x];
    tagc[x] = 0;
  }
}
void build(int x, int l, int r) {
  if (l == r) {
    mn[x] = r - l + 1, s[x] = 1;
    return;
  }
  int mid = (l + r) >> 1;
  build(x << 1, l, mid);
  build(x << 1 | 1, mid + 1, r);
  up(x);
}
void update(int x, int l, int r, int L, int R, int y) {
  if (l >= L && r <= R) {
    mn[x] += y, tag[x] += y;
    return;
  }
  down(x);
  int mid = (l + r) >> 1;
  if (R <= mid)
    update(x << 1, l, mid, L, R, y);
  else if (L > mid)
    update(x << 1 | 1, mid + 1, r, L, R, y);
  else
    update(x << 1, l, mid, L, mid, y),
        update(x << 1 | 1, mid + 1, r, mid + 1, R, y);
  up(x);
}
long long query(int x, int l, int r, int L, int R) {
  if (l >= L && r <= R) return c[x];
  down(x);
  int mid = (l + r) >> 1;
  if (R <= mid)
    return query(x << 1, l, mid, L, R);
  else if (L > mid)
    return query(x << 1 | 1, mid + 1, r, L, R);
  else
    return query(x << 1, l, mid, L, mid) +
           query(x << 1 | 1, mid + 1, r, mid + 1, R);
}
struct Qnode {
  int l, r, id;
} Q[MAXN];
signed main() {
  int n, m;
  read(n);
  for (int i = 1; i <= n; ++i) read(p[i]);
  read(m);
  for (int i = 1; i <= m; ++i) read(Q[i].l), read(Q[i].r), Q[i].id = i;
  sort(Q + 1, Q + m + 1, [&](Qnode x, Qnode y) { return x.r < y.r; });
  build(1, 1, n);
  for (int i = 1, topmx = 0, topmn = 0, nw = 1; i <= n; ++i) {
    while (topmx && p[stkmx[topmx]] < p[i])
      update(1, 1, n, stkmx[topmx - 1] + 1, stkmx[topmx], -p[stkmx[topmx]]),
          --topmx;
    while (topmn && p[stkmn[topmn]] > p[i])
      update(1, 1, n, stkmn[topmn - 1] + 1, stkmn[topmn], p[stkmn[topmn]]),
          --topmn;
    update(1, 1, n, 1, i, -1);
    update(1, 1, n, stkmx[topmx] + 1, i, p[i]);
    update(1, 1, n, stkmn[topmn] + 1, i, -p[i]);
    stkmx[++topmx] = i;
    stkmn[++topmn] = i;
    down(1), ++tagc[1], c[1] += s[1];
    while (nw <= m && Q[nw].r == i)
      Ans[Q[nw].id] = query(1, 1, n, Q[nw].l, i), ++nw;
  }
  for (int i = 1; i <= m; ++i) print(Ans[i]), putc('\n');
  return 0;
}

### Prompt

In CPP, your task is to solve 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>
inline bool upmin(T &x, T y) {
  return y < x ? x = y, 1 : 0;
}
template <typename T>
inline bool upmax(T &x, T y) {
  return x < y ? x = y, 1 : 0;
}
const long double eps = 1e-9;
const long double pi = acos(-1);
const int oo = 1 << 30;
const long long loo = 1ll << 62;
const int mods = 1e9 + 7;
const int MAXN = 300005;
const int INF = 0x3f3f3f3f;
namespace FastIO {
constexpr int SIZE = (1 << 21) + 1;
int num = 0, f;
char ibuf[SIZE], obuf[SIZE], que[65], *iS, *iT, *oS = obuf,
                                                *oT = obuf + SIZE - 1, c;
inline void flush() {
  fwrite(obuf, 1, oS - obuf, stdout);
  oS = obuf;
}
inline void putc(char c) {
  *oS++ = c;
  if (oS == oT) flush();
}
inline void getc(char &c) {
  for (c = (iS == iT ? (iT = ((iS = ibuf) + fread(ibuf, 1, SIZE, stdin)),
                       (iS == iT ? EOF : *iS++))
                     : *iS++);
       c < 'A' || c > 'Z';
       c = (iS == iT ? (iT = ((iS = ibuf) + fread(ibuf, 1, SIZE, stdin)),
                       (iS == iT ? EOF : *iS++))
                     : *iS++))
    ;
}
template <class I>
inline void read(I &x) {
  for (f = 1, c = (iS == iT ? (iT = ((iS = ibuf) + fread(ibuf, 1, SIZE, stdin)),
                              (iS == iT ? EOF : *iS++))
                            : *iS++);
       c < '0' || c > '9';
       c = (iS == iT ? (iT = ((iS = ibuf) + fread(ibuf, 1, SIZE, stdin)),
                       (iS == iT ? EOF : *iS++))
                     : *iS++))
    if (c == '-') f = -1;
  for (x = 0; c >= '0' && c <= '9';
       c = (iS == iT ? (iT = ((iS = ibuf) + fread(ibuf, 1, SIZE, stdin)),
                       (iS == iT ? EOF : *iS++))
                     : *iS++))
    x = (x << 3) + (x << 1) + (c & 15);
  x *= f;
}
template <class I>
inline void print(I x) {
  if (x < 0) putc('-'), x = -x;
  if (!x) putc('0');
  while (x) que[++num] = x % 10 + 48, x /= 10;
  while (num) putc(que[num--]);
}
struct Flusher_ {
  ~Flusher_() { flush(); }
} io_Flusher_;
}  // namespace FastIO
using FastIO ::print;
using FastIO ::putc;
using FastIO ::read;
long long Ans[MAXN], c[MAXN << 2], tagc[MAXN << 2];
int MX = 0, tag[MAXN << 2], mn[MAXN << 2], s[MAXN << 2], stkmx[MAXN],
    stkmn[MAXN], p[MAXN];
void up(int x) {
  mn[x] = min(mn[x << 1], mn[x << 1 | 1]);
  s[x] = (mn[x << 1] == mn[x]) * s[x << 1] +
         (mn[x << 1 | 1] == mn[x]) * s[x << 1 | 1];
}
void Down(int x) {
  if (tag[x] != 0) {
    mn[x << 1] += tag[x], mn[x << 1 | 1] += tag[x];
    tag[x << 1] += tag[x], tag[x << 1 | 1] += tag[x];
    tag[x] = 0;
  }
}
void down(int x) {
  Down(x);
  if (tagc[x] != 0) {
    if (mn[x << 1] == mn[x])
      c[x << 1] += tagc[x] * s[x << 1], tagc[x << 1] += tagc[x];
    if (mn[x << 1 | 1] == mn[x])
      c[x << 1 | 1] += tagc[x] * s[x << 1 | 1], tagc[x << 1 | 1] += tagc[x];
    tagc[x] = 0;
  }
}
void build(int x, int l, int r) {
  if (l == r) {
    mn[x] = r - l + 1, s[x] = 1;
    return;
  }
  int mid = (l + r) >> 1;
  build(x << 1, l, mid);
  build(x << 1 | 1, mid + 1, r);
  up(x);
}
void update(int x, int l, int r, int L, int R, int y) {
  if (l >= L && r <= R) {
    mn[x] += y, tag[x] += y;
    return;
  }
  down(x);
  int mid = (l + r) >> 1;
  if (R <= mid)
    update(x << 1, l, mid, L, R, y);
  else if (L > mid)
    update(x << 1 | 1, mid + 1, r, L, R, y);
  else
    update(x << 1, l, mid, L, mid, y),
        update(x << 1 | 1, mid + 1, r, mid + 1, R, y);
  up(x);
}
long long query(int x, int l, int r, int L, int R) {
  if (l >= L && r <= R) return c[x];
  down(x);
  int mid = (l + r) >> 1;
  if (R <= mid)
    return query(x << 1, l, mid, L, R);
  else if (L > mid)
    return query(x << 1 | 1, mid + 1, r, L, R);
  else
    return query(x << 1, l, mid, L, mid) +
           query(x << 1 | 1, mid + 1, r, mid + 1, R);
}
struct Qnode {
  int l, r, id;
} Q[MAXN];
signed main() {
  int n, m;
  read(n);
  for (int i = 1; i <= n; ++i) read(p[i]);
  read(m);
  for (int i = 1; i <= m; ++i) read(Q[i].l), read(Q[i].r), Q[i].id = i;
  sort(Q + 1, Q + m + 1, [&](Qnode x, Qnode y) { return x.r < y.r; });
  build(1, 1, n);
  for (int i = 1, topmx = 0, topmn = 0, nw = 1; i <= n; ++i) {
    while (topmx && p[stkmx[topmx]] < p[i])
      update(1, 1, n, stkmx[topmx - 1] + 1, stkmx[topmx], -p[stkmx[topmx]]),
          --topmx;
    while (topmn && p[stkmn[topmn]] > p[i])
      update(1, 1, n, stkmn[topmn - 1] + 1, stkmn[topmn], p[stkmn[topmn]]),
          --topmn;
    update(1, 1, n, 1, i, -1);
    update(1, 1, n, stkmx[topmx] + 1, i, p[i]);
    update(1, 1, n, stkmn[topmn] + 1, i, -p[i]);
    stkmx[++topmx] = i;
    stkmn[++topmn] = i;
    down(1), ++tagc[1], c[1] += s[1];
    while (nw <= m && Q[nw].r == i)
      Ans[Q[nw].id] = query(1, 1, n, Q[nw].l, i), ++nw;
  }
  for (int i = 1; i <= m; ++i) print(Ans[i]), putc('\n');
  return 0;
}

```