#include <bits/stdc++.h>
const int inf = 1e9;
long long ans[300005];
struct segmentTree {
  int inc[300005 << 2], tag[300005 << 2] = {0}, min[300005 << 2] = {0},
                                      cnt[300005 << 2] = {0};
  long long sum[300005 << 2];
  void pushdown(int x) {
    tag[x << 1] += tag[x];
    tag[x << 1 | 1] += tag[x];
    min[x << 1] += tag[x];
    min[x << 1 | 1] += tag[x];
    sum[x << 1] +=
        (long long)inc[x] * (min[x << 1] == min[x] ? cnt[x << 1] : 0);
    sum[x << 1 | 1] +=
        (long long)inc[x] * (min[x << 1 | 1] == min[x] ? cnt[x << 1 | 1] : 0);
    if (min[x << 1] == min[x]) inc[x << 1] += inc[x];
    if (min[x << 1 | 1] == min[x]) inc[x << 1 | 1] += inc[x];
    tag[x] = inc[x] = 0;
  }
  void update(int x) {
    if (min[x << 1] < min[x << 1 | 1])
      cnt[x] = cnt[x << 1];
    else if (min[x << 1] > min[x << 1 | 1])
      cnt[x] = cnt[x << 1 | 1];
    else
      cnt[x] = cnt[x << 1] + cnt[x << 1 | 1];
    min[x] = std::min(min[x << 1], min[x << 1 | 1]);
    sum[x] = sum[x << 1] + sum[x << 1 | 1];
  }
  void build(int l, int r, int rt) {
    tag[rt] = 0;
    if (l == r) {
      min[rt] = inf;
      cnt[rt] = 1;
      return;
    }
    build(l, (l + r) >> 1, rt << 1);
    build(((l + r) >> 1) + 1, r, rt << 1 | 1);
    update(rt);
  }
  void add(int l, int r, int L, int R, int v, int rt) {
    if (l > R || r < L) return;
    if (l <= L && R <= r) {
      tag[rt] += v;
      min[rt] += v;
      return;
    }
    pushdown(rt);
    add(l, r, L, (L + R) >> 1, v, rt << 1);
    add(l, r, ((L + R) >> 1) + 1, R, v, rt << 1 | 1);
    update(rt);
  }
  long long query(int l, int r, int L, int R, int rt) {
    if (l > R || r < L) return 0;
    if (l <= L && R <= r) return sum[rt];
    pushdown(rt);
    return query(l, r, L, (L + R) >> 1, rt << 1) +
           query(l, r, ((L + R) >> 1) + 1, R, rt << 1 | 1);
  }
} A;
int n, x, y, a[300005], l[300005], r[300005], q;
std::vector<int> rec[300005];
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; ++i) scanf("%d", &a[i]);
  scanf("%d", &q);
  for (int i = 1; i <= q; ++i) {
    scanf("%d%d", &l[i], &r[i]);
    rec[r[i]].push_back(i);
  }
  std::stack<int> min, max;
  min.push(0);
  max.push(0);
  A.build(1, n, 1);
  for (int i = 1; i <= n; ++i) {
    while (min.top() && a[i] < a[min.top()]) {
      int p = min.top();
      min.pop();
      A.add(min.top() + 1, p, 1, n, -a[i] + a[p], 1);
    }
    while (max.top() && a[i] > a[max.top()]) {
      int p = max.top();
      max.pop();
      A.add(max.top() + 1, p, 1, n, a[i] - a[p], 1);
    }
    if (i > 1) A.add(1, i - 1, 1, n, (i - 1), 1);
    A.add(1, i, 1, n, -i, 1);
    A.add(i, i, 1, n, -inf + i, 1);
    max.push(i);
    min.push(i);
    A.inc[1]++;
    A.sum[1] += A.cnt[1];
    for (int x : rec[i]) {
      ans[x] = A.query(l[x], r[x], 1, n, 1);
    }
  }
  for (int i = 1; i <= q; ++i) printf("%lld\n", ans[i]);
  return 0;
}
