#include <bits/stdc++.h>
namespace MyMinMax {
template <typename T>
inline T mn(const T x, const T y) {
  return x < y ? x : y;
}
template <typename T>
inline T mx(const T x, const T y) {
  return x > y ? x : y;
}
template <typename T>
inline bool chmin(T &x, const T y) {
  return (x > y) && ((x = y), 1);
}
template <typename T>
inline bool chmax(T &x, const T y) {
  return (x < y) && ((x = y), 1);
}
template <typename T, typename... Args>
inline T mx(const T x, const Args... args) {
  return mx(x, mx(args...));
}
template <typename T, typename... Args>
inline T mn(const T x, const Args... args) {
  return mn(x, mn(args...));
}
}  // namespace MyMinMax
using namespace MyMinMax;
namespace IO {
const int DPAIRSIZ = 1 << 18;
char BB[DPAIRSIZ], *SS = BB, *TT = BB;
inline char getcha() {
  return SS == TT && (TT = (SS = BB) + fread(BB, 1, DPAIRSIZ, stdin), SS == TT)
             ? EOF
             : *SS++;
}
template <typename T = int>
inline T read() {
  T x = 0;
  int fu = 1;
  char c = getcha();
  while (c > 57 || c < 48) {
    if (c == 45) fu = -1;
    c = getcha();
  }
  while (c <= 57 && c >= 48) {
    x = x * 10 + c - 48;
    c = getcha();
  }
  x *= fu;
  return x;
}
template <typename T>
inline void read(T &x) {
  x = 0;
  int fu = 1;
  char c = getcha();
  while (c > 57 || c < 48) {
    if (c == 45) fu = -1;
    c = getcha();
  }
  while (c <= 57 && c >= 48) {
    x = x * 10 + c - 48;
    c = getcha();
  }
  x *= fu;
}
template <typename T>
inline void read(T *bg, T *ed) {
  while (bg != ed) read(*bg++);
}
inline void read(char &ch) {
  ch = getcha();
  while (ch <= 32) ch = getcha();
}
inline void read(char *s) {
  char ch = getcha();
  while (ch <= 32) ch = getcha();
  while (ch > 32) *s++ = ch, ch = getcha();
  *s = '\0';
}
inline void sread(char *s) {
  char ch = getcha();
  while (ch < 32) ch = getcha();
  while (ch >= 32) *s++ = ch, ch = getcha();
  *s = '\0';
}
inline void pread(char *&s) {
  char ch = getcha();
  while (ch <= 32) ch = getcha();
  while (ch > 32) *s++ = ch, ch = getcha();
  *s = '\0';
}
inline void spread(char *&s) {
  char ch = getcha();
  while (ch < 32) ch = getcha();
  while (ch >= 32) *s++ = ch, ch = getcha();
  *s = '\0';
}
template <typename T, typename... Args>
inline void read(T &x, Args &...args) {
  read(x);
  read(args...);
}
char out[DPAIRSIZ], *Out = out;
inline void putcha(char x) {
  *Out++ = x;
  if (Out - out >= (DPAIRSIZ)) fwrite(out, 1, Out - out, stdout), Out = out;
}
template <typename T>
inline void fprint(T x) {
  if (x < 0) putcha(45), x = -x;
  if (x > 9) fprint(x / 10);
  putcha(x % 10 + 48);
}
inline void print() { putcha(10); }
template <typename T>
inline void print(T x) {
  fprint(x);
  putcha(10);
}
inline void print(char *ch) {
  while (*ch != '\0') putcha(*(ch++));
  putcha(10);
}
inline void put(char *ch) {
  while (*ch != '\0') putcha(*(ch++));
}
inline void print(const char *ch) {
  while (*ch != '\0') putcha(*(ch++));
  putcha(10);
}
inline void put(const char *ch) {
  while (*ch != '\0') putcha(*(ch++));
}
template <typename T, typename... Args>
inline void print(T x, Args... args) {
  fprint(x);
  putcha(32);
  print(args...);
}
template <typename... Args>
inline void print(const char *ch, Args... args) {
  while (*ch != '\0') putcha(*(ch++));
  putcha(32);
  print(args...);
}
template <typename... Args>
inline void print(char *ch, Args... args) {
  while (*ch != '\0') putcha(*(ch++));
  putcha(32);
  print(args...);
}
template <typename T, typename... Args>
inline void printl(T x, Args... args) {
  fprint(x);
  putcha(10);
  printl(args...);
}
template <typename... Args>
inline void printl(const char *ch, Args... args) {
  while (*ch != '\0') putcha(*(ch++));
  putcha(10);
  printl(args...);
}
template <typename... Args>
inline void printl(char *ch, Args... args) {
  while (*ch != '\0') putcha(*(ch++));
  putcha(10);
  printl(args...);
}
template <typename T>
inline void sprint(T x) {
  fprint(x);
  putcha(32);
}
template <typename T, typename... Args>
inline void sprint(T x, Args... args) {
  fprint(x);
  putcha(32);
  sprint(args...);
}
template <typename T>
inline void sprint(T *bg, T *ed) {
  while (bg != ed) sprint(*bg++);
}
template <typename T>
inline void print(T *bg, T *ed) {
  while (bg != ed) sprint(*bg++);
  putcha(10);
}
template <typename T>
inline void printl(T *bg, T *ed) {
  while (bg != ed) print(*bg++);
}
class AutoFlush {
 public:
  ~AutoFlush() { fwrite(out, 1, Out - out, stdout); }
} __AutoFlush;
}  // namespace IO
using namespace IO;
const int INF = 0x3f3f3f3f;
const long long INFll = 0x3f3f3f3f3f3f3f3fll;
const int MAXN = 1.2e5 + 5;
int n, m, a[MAXN];
long long ans[MAXN];
struct Quest {
  int l, r, id;
  inline bool operator<(const Quest &tmp) const { return r < tmp.r; }
} q[MAXN];
struct Segment_tree {
  long long t[MAXN << 2];
  int tag[MAXN << 2], tim[MAXN << 2], s[MAXN << 2], p[MAXN << 2];
  inline void pushup(int rt) {
    t[rt] = t[rt << 1] + t[rt << 1 | 1];
    p[rt] = mn(p[rt << 1], p[rt << 1 | 1]);
    s[rt] = (p[rt << 1] == p[rt]) * s[rt << 1];
    s[rt] += (p[rt << 1 | 1] == p[rt]) * s[rt << 1 | 1];
  }
  void build(int rt, int l, int r) {
    if (l == r) {
      s[rt] = 1;
      p[rt] = l;
      return;
    }
    int mid = (l + r) >> 1;
    build(rt << 1, l, mid);
    build(rt << 1 | 1, mid + 1, r);
    pushup(rt);
  }
  inline void update(int rt, int z) {
    tag[rt] += z;
    p[rt] += z;
  }
  inline void Update(int rt, int z) {
    t[rt] += 1ll * z * s[rt];
    tim[rt] += z;
  }
  inline void pushdown(int rt) {
    if (tag[rt])
      update(rt << 1, tag[rt]), update(rt << 1 | 1, tag[rt]), tag[rt] = 0;
    if (tim[rt]) {
      if (p[rt << 1] == p[rt]) Update(rt << 1, tim[rt]);
      if (p[rt << 1 | 1] == p[rt]) Update(rt << 1 | 1, tim[rt]);
      tim[rt] = 0;
    }
  }
  void modify(int rt, int l, int r, int x, int y, int z) {
    if (x <= l && r <= y) return update(rt, z);
    pushdown(rt);
    int mid = (l + r) >> 1;
    if (x <= mid) modify(rt << 1, l, mid, x, y, z);
    if (y > mid) modify(rt << 1 | 1, mid + 1, r, x, y, z);
    pushup(rt);
  }
  long long query(int rt, int l, int r, int x, int y) {
    if (x <= l && r <= y) return t[rt];
    pushdown(rt);
    int mid = (l + r) >> 1;
    if (x > mid) return query(rt << 1 | 1, mid + 1, r, x, y);
    if (y <= mid) return query(rt << 1, l, mid, x, y);
    return query(rt << 1, l, mid, x, y) + query(rt << 1 | 1, mid + 1, r, x, y);
  }
} t;
int s1[MAXN], s2[MAXN];
signed main() {
  read(n);
  read(a + 1, a + n + 1);
  read(m);
  for (int i = (1), iEND = (m); i <= iEND; ++i) {
    read(q[i].l, q[i].r);
    q[i].id = i;
  }
  t.build(1, 1, n);
  std::sort(q + 1, q + m + 1);
  int it = 0, t1 = 0, t2 = 0;
  for (int i = (1), iEND = (m); i <= iEND; ++i) {
    while (it < q[i].r) {
      ++it;
      while (t1 && a[s1[t1]] < a[it])
        t.modify(1, 1, n, s1[t1 - 1] + 1, s1[t1], a[it] - a[s1[t1]]), --t1;
      while (t2 && a[s2[t2]] > a[it])
        t.modify(1, 1, n, s2[t2 - 1] + 1, s2[t2], a[s2[t2]] - a[it]), --t2;
      s1[++t1] = s2[++t2] = it;
      t.update(1, -1);
      t.Update(1, 1);
    }
    ans[q[i].id] = t.query(1, 1, n, q[i].l, q[i].r);
  }
  printl(ans + 1, ans + m + 1);
}

### Prompt

Generate 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>
namespace MyMinMax {
template <typename T>
inline T mn(const T x, const T y) {
  return x < y ? x : y;
}
template <typename T>
inline T mx(const T x, const T y) {
  return x > y ? x : y;
}
template <typename T>
inline bool chmin(T &x, const T y) {
  return (x > y) && ((x = y), 1);
}
template <typename T>
inline bool chmax(T &x, const T y) {
  return (x < y) && ((x = y), 1);
}
template <typename T, typename... Args>
inline T mx(const T x, const Args... args) {
  return mx(x, mx(args...));
}
template <typename T, typename... Args>
inline T mn(const T x, const Args... args) {
  return mn(x, mn(args...));
}
}  // namespace MyMinMax
using namespace MyMinMax;
namespace IO {
const int DPAIRSIZ = 1 << 18;
char BB[DPAIRSIZ], *SS = BB, *TT = BB;
inline char getcha() {
  return SS == TT && (TT = (SS = BB) + fread(BB, 1, DPAIRSIZ, stdin), SS == TT)
             ? EOF
             : *SS++;
}
template <typename T = int>
inline T read() {
  T x = 0;
  int fu = 1;
  char c = getcha();
  while (c > 57 || c < 48) {
    if (c == 45) fu = -1;
    c = getcha();
  }
  while (c <= 57 && c >= 48) {
    x = x * 10 + c - 48;
    c = getcha();
  }
  x *= fu;
  return x;
}
template <typename T>
inline void read(T &x) {
  x = 0;
  int fu = 1;
  char c = getcha();
  while (c > 57 || c < 48) {
    if (c == 45) fu = -1;
    c = getcha();
  }
  while (c <= 57 && c >= 48) {
    x = x * 10 + c - 48;
    c = getcha();
  }
  x *= fu;
}
template <typename T>
inline void read(T *bg, T *ed) {
  while (bg != ed) read(*bg++);
}
inline void read(char &ch) {
  ch = getcha();
  while (ch <= 32) ch = getcha();
}
inline void read(char *s) {
  char ch = getcha();
  while (ch <= 32) ch = getcha();
  while (ch > 32) *s++ = ch, ch = getcha();
  *s = '\0';
}
inline void sread(char *s) {
  char ch = getcha();
  while (ch < 32) ch = getcha();
  while (ch >= 32) *s++ = ch, ch = getcha();
  *s = '\0';
}
inline void pread(char *&s) {
  char ch = getcha();
  while (ch <= 32) ch = getcha();
  while (ch > 32) *s++ = ch, ch = getcha();
  *s = '\0';
}
inline void spread(char *&s) {
  char ch = getcha();
  while (ch < 32) ch = getcha();
  while (ch >= 32) *s++ = ch, ch = getcha();
  *s = '\0';
}
template <typename T, typename... Args>
inline void read(T &x, Args &...args) {
  read(x);
  read(args...);
}
char out[DPAIRSIZ], *Out = out;
inline void putcha(char x) {
  *Out++ = x;
  if (Out - out >= (DPAIRSIZ)) fwrite(out, 1, Out - out, stdout), Out = out;
}
template <typename T>
inline void fprint(T x) {
  if (x < 0) putcha(45), x = -x;
  if (x > 9) fprint(x / 10);
  putcha(x % 10 + 48);
}
inline void print() { putcha(10); }
template <typename T>
inline void print(T x) {
  fprint(x);
  putcha(10);
}
inline void print(char *ch) {
  while (*ch != '\0') putcha(*(ch++));
  putcha(10);
}
inline void put(char *ch) {
  while (*ch != '\0') putcha(*(ch++));
}
inline void print(const char *ch) {
  while (*ch != '\0') putcha(*(ch++));
  putcha(10);
}
inline void put(const char *ch) {
  while (*ch != '\0') putcha(*(ch++));
}
template <typename T, typename... Args>
inline void print(T x, Args... args) {
  fprint(x);
  putcha(32);
  print(args...);
}
template <typename... Args>
inline void print(const char *ch, Args... args) {
  while (*ch != '\0') putcha(*(ch++));
  putcha(32);
  print(args...);
}
template <typename... Args>
inline void print(char *ch, Args... args) {
  while (*ch != '\0') putcha(*(ch++));
  putcha(32);
  print(args...);
}
template <typename T, typename... Args>
inline void printl(T x, Args... args) {
  fprint(x);
  putcha(10);
  printl(args...);
}
template <typename... Args>
inline void printl(const char *ch, Args... args) {
  while (*ch != '\0') putcha(*(ch++));
  putcha(10);
  printl(args...);
}
template <typename... Args>
inline void printl(char *ch, Args... args) {
  while (*ch != '\0') putcha(*(ch++));
  putcha(10);
  printl(args...);
}
template <typename T>
inline void sprint(T x) {
  fprint(x);
  putcha(32);
}
template <typename T, typename... Args>
inline void sprint(T x, Args... args) {
  fprint(x);
  putcha(32);
  sprint(args...);
}
template <typename T>
inline void sprint(T *bg, T *ed) {
  while (bg != ed) sprint(*bg++);
}
template <typename T>
inline void print(T *bg, T *ed) {
  while (bg != ed) sprint(*bg++);
  putcha(10);
}
template <typename T>
inline void printl(T *bg, T *ed) {
  while (bg != ed) print(*bg++);
}
class AutoFlush {
 public:
  ~AutoFlush() { fwrite(out, 1, Out - out, stdout); }
} __AutoFlush;
}  // namespace IO
using namespace IO;
const int INF = 0x3f3f3f3f;
const long long INFll = 0x3f3f3f3f3f3f3f3fll;
const int MAXN = 1.2e5 + 5;
int n, m, a[MAXN];
long long ans[MAXN];
struct Quest {
  int l, r, id;
  inline bool operator<(const Quest &tmp) const { return r < tmp.r; }
} q[MAXN];
struct Segment_tree {
  long long t[MAXN << 2];
  int tag[MAXN << 2], tim[MAXN << 2], s[MAXN << 2], p[MAXN << 2];
  inline void pushup(int rt) {
    t[rt] = t[rt << 1] + t[rt << 1 | 1];
    p[rt] = mn(p[rt << 1], p[rt << 1 | 1]);
    s[rt] = (p[rt << 1] == p[rt]) * s[rt << 1];
    s[rt] += (p[rt << 1 | 1] == p[rt]) * s[rt << 1 | 1];
  }
  void build(int rt, int l, int r) {
    if (l == r) {
      s[rt] = 1;
      p[rt] = l;
      return;
    }
    int mid = (l + r) >> 1;
    build(rt << 1, l, mid);
    build(rt << 1 | 1, mid + 1, r);
    pushup(rt);
  }
  inline void update(int rt, int z) {
    tag[rt] += z;
    p[rt] += z;
  }
  inline void Update(int rt, int z) {
    t[rt] += 1ll * z * s[rt];
    tim[rt] += z;
  }
  inline void pushdown(int rt) {
    if (tag[rt])
      update(rt << 1, tag[rt]), update(rt << 1 | 1, tag[rt]), tag[rt] = 0;
    if (tim[rt]) {
      if (p[rt << 1] == p[rt]) Update(rt << 1, tim[rt]);
      if (p[rt << 1 | 1] == p[rt]) Update(rt << 1 | 1, tim[rt]);
      tim[rt] = 0;
    }
  }
  void modify(int rt, int l, int r, int x, int y, int z) {
    if (x <= l && r <= y) return update(rt, z);
    pushdown(rt);
    int mid = (l + r) >> 1;
    if (x <= mid) modify(rt << 1, l, mid, x, y, z);
    if (y > mid) modify(rt << 1 | 1, mid + 1, r, x, y, z);
    pushup(rt);
  }
  long long query(int rt, int l, int r, int x, int y) {
    if (x <= l && r <= y) return t[rt];
    pushdown(rt);
    int mid = (l + r) >> 1;
    if (x > mid) return query(rt << 1 | 1, mid + 1, r, x, y);
    if (y <= mid) return query(rt << 1, l, mid, x, y);
    return query(rt << 1, l, mid, x, y) + query(rt << 1 | 1, mid + 1, r, x, y);
  }
} t;
int s1[MAXN], s2[MAXN];
signed main() {
  read(n);
  read(a + 1, a + n + 1);
  read(m);
  for (int i = (1), iEND = (m); i <= iEND; ++i) {
    read(q[i].l, q[i].r);
    q[i].id = i;
  }
  t.build(1, 1, n);
  std::sort(q + 1, q + m + 1);
  int it = 0, t1 = 0, t2 = 0;
  for (int i = (1), iEND = (m); i <= iEND; ++i) {
    while (it < q[i].r) {
      ++it;
      while (t1 && a[s1[t1]] < a[it])
        t.modify(1, 1, n, s1[t1 - 1] + 1, s1[t1], a[it] - a[s1[t1]]), --t1;
      while (t2 && a[s2[t2]] > a[it])
        t.modify(1, 1, n, s2[t2 - 1] + 1, s2[t2], a[s2[t2]] - a[it]), --t2;
      s1[++t1] = s2[++t2] = it;
      t.update(1, -1);
      t.Update(1, 1);
    }
    ans[q[i].id] = t.query(1, 1, n, q[i].l, q[i].r);
  }
  printl(ans + 1, ans + m + 1);
}

```