#include <bits/stdc++.h>
using namespace std;
int n, p[120005], q;
namespace segT {
struct node {
  int mnv, mnc, tag, dlt;
  long long sum;
  int ans() { return mnv ? 0 : mnc; }
  void init() {
    mnc = tag = dlt = 0;
    mnv = (int)1e9;
    sum = 0ll;
  }
} arr[500005];
void init(int p, int l = 0, int r = n - 1) {
  arr[p].init();
  if (l < r) {
    int M = (l + r) / 2;
    init(p << 1, l, M);
    init(p << 1 | 1, M + 1, r);
  }
}
void pushdlt(int p, int l = 0, int r = n - 1) {
  if (arr[p].dlt) {
    arr[p].mnv += arr[p].dlt;
    if (l < r) {
      arr[p << 1].dlt += arr[p].dlt;
      arr[p << 1 | 1].dlt += arr[p].dlt;
    }
    arr[p].dlt = 0;
  }
}
void push(int p, int l = 0, int r = n - 1) {
  if (arr[p].tag) {
    arr[p].sum += (long long)arr[p].tag * (long long)arr[p].ans();
    if (l < r) {
      int M = (l + r) / 2;
      if (!arr[p << 1].dlt)
        arr[p << 1].tag += arr[p].tag;
      else if (arr[p << 1].dlt < 0) {
        pushdlt(p << 1, l, M);
        arr[p << 1].tag = arr[p].tag;
      }
      if (!arr[p << 1 | 1].dlt)
        arr[p << 1 | 1].tag += arr[p].tag;
      else if (arr[p << 1 | 1].dlt < 0) {
        pushdlt(p << 1 | 1, M + 1, r);
        arr[p << 1 | 1].tag = arr[p].tag;
      }
    }
    arr[p].tag = 0;
  }
  pushdlt(p, l, r);
}
void pull(int p, int l = 0, int r = n - 1) {
  if (l < r) {
    if (arr[p << 1].mnv == arr[p << 1 | 1].mnv) {
      arr[p].mnv = arr[p << 1].mnv;
      arr[p].mnc = arr[p << 1].mnc + arr[p << 1 | 1].mnc;
    } else if (arr[p << 1].mnv < arr[p << 1 | 1].mnv) {
      arr[p].mnv = arr[p << 1].mnv;
      arr[p].mnc = arr[p << 1].mnc;
    } else {
      arr[p].mnv = arr[p << 1 | 1].mnv;
      arr[p].mnc = arr[p << 1 | 1].mnc;
    }
  }
}
void activate(int p, int d, int V, int l = 0, int r = n - 1) {
  push(p, l, r);
  if (l == r) {
    arr[p].mnc = 1;
    arr[p].mnv = V;
    return;
  }
  int M = (l + r) / 2;
  if (d <= M) {
    activate(p << 1, d, V, l, M);
    push(p << 1 | 1, M + 1, r);
  } else {
    activate(p << 1 | 1, d, V, M + 1, r);
    push(p << 1, l, M);
  }
  pull(p, l, r);
}
void add(int p, int L, int R, int V, int l = 0, int r = n - 1) {
  if (l >= L && r <= R) {
    arr[p].dlt += V;
    push(p, l, r);
    return;
  }
  push(p, l, r);
  int M = (l + r) / 2;
  if (L <= M)
    add(p << 1, L, R, V, l, M);
  else
    push(p << 1, l, M);
  if (R > M)
    add(p << 1 | 1, L, R, V, M + 1, r);
  else
    push(p << 1 | 1, M + 1, r);
  pull(p, l, r);
}
long long qrysum(int p, int L, int R, int l = 0, int r = n - 1) {
  push(p, l, r);
  if (l >= L && r <= R) return arr[p].sum;
  int M = (l + r) / 2;
  long long ret = 0ll;
  if (L <= M) ret = qrysum(p << 1, L, R, l, M);
  if (R > M) ret += qrysum(p << 1 | 1, L, R, M + 1, r);
  return ret;
}
int qrycnt(int p, int L, int R, int l = 0, int r = n - 1) {
  push(p, l, r);
  if (l >= L && r <= R) return arr[p].ans();
  int M = (l + r) / 2, ret = 0;
  if (L <= M) ret = qrycnt(p << 1, L, R, l, M);
  if (R > M) ret += qrycnt(p << 1 | 1, L, R, M + 1, r);
  return ret;
}
};  // namespace segT
int stk[2][120005], pos[2][120005], sz[2], L[120005], R[120005];
long long res[120005];
vector<int> qs[120005];
int main() {
  int i, j, k;
  scanf("%d", &n);
  for (i = 0; i < n; ++i) {
    scanf("%d", p + i);
    --p[i];
  }
  scanf("%d", &q);
  for (i = 0; i < q; ++i) {
    scanf("%d%d", L + i, R + i);
    --L[i];
    --R[i];
    qs[R[i]].push_back(i);
  }
  sz[0] = sz[1] = 0;
  segT::init(1);
  for (i = 0; i < n; ++i) {
    segT::activate(1, i, 0);
    while (sz[0] && p[i] > stk[0][sz[0] - 1]) {
      segT::add(1, (sz[0] > 1 ? pos[0][sz[0] - 2] + 1 : 0), pos[0][sz[0] - 1],
                p[i] - stk[0][sz[0] - 1]);
      --sz[0];
    }
    stk[0][sz[0]] = p[i];
    pos[0][sz[0]++] = i;
    while (sz[1] && p[i] < stk[1][sz[1] - 1]) {
      segT::add(1, (sz[1] > 1 ? pos[1][sz[1] - 2] + 1 : 0), pos[1][sz[1] - 1],
                -p[i] + stk[1][sz[1] - 1]);
      --sz[1];
    }
    if (i) segT::add(1, 0, i - 1, -1);
    stk[1][sz[1]] = p[i];
    pos[1][sz[1]++] = i;
    segT::push(1);
    segT::arr[1].tag = 1;
    for (j = 0; j < (int)qs[i].size(); ++j) {
      res[qs[i][j]] = segT::qrysum(1, L[qs[i][j]], i);
    }
  }
  for (i = 0; i < q; ++i) printf("%lld\n", res[i]);
  return 0;
}

### 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>
using namespace std;
int n, p[120005], q;
namespace segT {
struct node {
  int mnv, mnc, tag, dlt;
  long long sum;
  int ans() { return mnv ? 0 : mnc; }
  void init() {
    mnc = tag = dlt = 0;
    mnv = (int)1e9;
    sum = 0ll;
  }
} arr[500005];
void init(int p, int l = 0, int r = n - 1) {
  arr[p].init();
  if (l < r) {
    int M = (l + r) / 2;
    init(p << 1, l, M);
    init(p << 1 | 1, M + 1, r);
  }
}
void pushdlt(int p, int l = 0, int r = n - 1) {
  if (arr[p].dlt) {
    arr[p].mnv += arr[p].dlt;
    if (l < r) {
      arr[p << 1].dlt += arr[p].dlt;
      arr[p << 1 | 1].dlt += arr[p].dlt;
    }
    arr[p].dlt = 0;
  }
}
void push(int p, int l = 0, int r = n - 1) {
  if (arr[p].tag) {
    arr[p].sum += (long long)arr[p].tag * (long long)arr[p].ans();
    if (l < r) {
      int M = (l + r) / 2;
      if (!arr[p << 1].dlt)
        arr[p << 1].tag += arr[p].tag;
      else if (arr[p << 1].dlt < 0) {
        pushdlt(p << 1, l, M);
        arr[p << 1].tag = arr[p].tag;
      }
      if (!arr[p << 1 | 1].dlt)
        arr[p << 1 | 1].tag += arr[p].tag;
      else if (arr[p << 1 | 1].dlt < 0) {
        pushdlt(p << 1 | 1, M + 1, r);
        arr[p << 1 | 1].tag = arr[p].tag;
      }
    }
    arr[p].tag = 0;
  }
  pushdlt(p, l, r);
}
void pull(int p, int l = 0, int r = n - 1) {
  if (l < r) {
    if (arr[p << 1].mnv == arr[p << 1 | 1].mnv) {
      arr[p].mnv = arr[p << 1].mnv;
      arr[p].mnc = arr[p << 1].mnc + arr[p << 1 | 1].mnc;
    } else if (arr[p << 1].mnv < arr[p << 1 | 1].mnv) {
      arr[p].mnv = arr[p << 1].mnv;
      arr[p].mnc = arr[p << 1].mnc;
    } else {
      arr[p].mnv = arr[p << 1 | 1].mnv;
      arr[p].mnc = arr[p << 1 | 1].mnc;
    }
  }
}
void activate(int p, int d, int V, int l = 0, int r = n - 1) {
  push(p, l, r);
  if (l == r) {
    arr[p].mnc = 1;
    arr[p].mnv = V;
    return;
  }
  int M = (l + r) / 2;
  if (d <= M) {
    activate(p << 1, d, V, l, M);
    push(p << 1 | 1, M + 1, r);
  } else {
    activate(p << 1 | 1, d, V, M + 1, r);
    push(p << 1, l, M);
  }
  pull(p, l, r);
}
void add(int p, int L, int R, int V, int l = 0, int r = n - 1) {
  if (l >= L && r <= R) {
    arr[p].dlt += V;
    push(p, l, r);
    return;
  }
  push(p, l, r);
  int M = (l + r) / 2;
  if (L <= M)
    add(p << 1, L, R, V, l, M);
  else
    push(p << 1, l, M);
  if (R > M)
    add(p << 1 | 1, L, R, V, M + 1, r);
  else
    push(p << 1 | 1, M + 1, r);
  pull(p, l, r);
}
long long qrysum(int p, int L, int R, int l = 0, int r = n - 1) {
  push(p, l, r);
  if (l >= L && r <= R) return arr[p].sum;
  int M = (l + r) / 2;
  long long ret = 0ll;
  if (L <= M) ret = qrysum(p << 1, L, R, l, M);
  if (R > M) ret += qrysum(p << 1 | 1, L, R, M + 1, r);
  return ret;
}
int qrycnt(int p, int L, int R, int l = 0, int r = n - 1) {
  push(p, l, r);
  if (l >= L && r <= R) return arr[p].ans();
  int M = (l + r) / 2, ret = 0;
  if (L <= M) ret = qrycnt(p << 1, L, R, l, M);
  if (R > M) ret += qrycnt(p << 1 | 1, L, R, M + 1, r);
  return ret;
}
};  // namespace segT
int stk[2][120005], pos[2][120005], sz[2], L[120005], R[120005];
long long res[120005];
vector<int> qs[120005];
int main() {
  int i, j, k;
  scanf("%d", &n);
  for (i = 0; i < n; ++i) {
    scanf("%d", p + i);
    --p[i];
  }
  scanf("%d", &q);
  for (i = 0; i < q; ++i) {
    scanf("%d%d", L + i, R + i);
    --L[i];
    --R[i];
    qs[R[i]].push_back(i);
  }
  sz[0] = sz[1] = 0;
  segT::init(1);
  for (i = 0; i < n; ++i) {
    segT::activate(1, i, 0);
    while (sz[0] && p[i] > stk[0][sz[0] - 1]) {
      segT::add(1, (sz[0] > 1 ? pos[0][sz[0] - 2] + 1 : 0), pos[0][sz[0] - 1],
                p[i] - stk[0][sz[0] - 1]);
      --sz[0];
    }
    stk[0][sz[0]] = p[i];
    pos[0][sz[0]++] = i;
    while (sz[1] && p[i] < stk[1][sz[1] - 1]) {
      segT::add(1, (sz[1] > 1 ? pos[1][sz[1] - 2] + 1 : 0), pos[1][sz[1] - 1],
                -p[i] + stk[1][sz[1] - 1]);
      --sz[1];
    }
    if (i) segT::add(1, 0, i - 1, -1);
    stk[1][sz[1]] = p[i];
    pos[1][sz[1]++] = i;
    segT::push(1);
    segT::arr[1].tag = 1;
    for (j = 0; j < (int)qs[i].size(); ++j) {
      res[qs[i][j]] = segT::qrysum(1, L[qs[i][j]], i);
    }
  }
  for (i = 0; i < q; ++i) printf("%lld\n", res[i]);
  return 0;
}

```