#include <bits/stdc++.h>
using namespace std;
int _max(int x, int y) { return x > y ? x : y; }
int _min(int x, int y) { return x < y ? x : y; }
int read() {
  int s = 0, f = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-') f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') s = s * 10 + ch - '0', ch = getchar();
  return s * f;
}
struct tnode {
  int l, r, lc, rc, c, ms, lazy, ad;
  long long ans;
} t[250000];
int cnt;
struct node {
  int l, r, id;
} q[130000];
int a[130000];
int tpmn, tpmx, stamn[130000], stamx[130000];
long long ans[130000];
bool cmp(node a, node b) { return a.r < b.r; }
void bt(int l, int r) {
  int now = ++cnt;
  t[now].l = l, t[now].r = r;
  t[now].lc = t[now].rc = -1;
  t[now].c = l;
  t[now].lazy = 0;
  t[now].ms = 1;
  if (l < r) {
    int mid = (l + r) / 2;
    t[now].lc = cnt + 1;
    bt(l, mid);
    t[now].rc = cnt + 1;
    bt(mid + 1, r);
    t[now].ms = t[t[now].lc].ms + t[t[now].rc].ms;
  }
}
void pushdown(int now) {
  int lc = t[now].lc, rc = t[now].rc;
  t[lc].c += t[now].lazy, t[lc].lazy += t[now].lazy;
  t[rc].c += t[now].lazy;
  t[rc].lazy += t[now].lazy;
  if (t[lc].c == t[now].c)
    t[lc].ans += (long long)t[lc].ms * t[now].ad, t[lc].ad += t[now].ad;
  if (t[rc].c == t[now].c)
    t[rc].ans += (long long)t[rc].ms * t[now].ad, t[rc].ad += t[now].ad;
  t[now].lazy = t[now].ad = 0;
}
void update(int now) {
  int lc = t[now].lc, rc = t[now].rc;
  if (t[lc].c < t[rc].c)
    t[now].c = t[lc].c, t[now].ms = t[lc].ms;
  else if (t[lc].c > t[rc].c)
    t[now].c = t[rc].c, t[now].ms = t[rc].ms;
  else
    t[now].c = t[lc].c, t[now].ms = t[lc].ms + t[rc].ms;
  t[now].ans = t[lc].ans + t[rc].ans;
}
void add(int now, int l, int r, int c) {
  if (t[now].l == l && t[now].r == r) {
    t[now].c += c, t[now].lazy += c;
    return;
  }
  pushdown(now);
  int mid = (t[now].l + t[now].r) / 2;
  if (r <= mid)
    add(t[now].lc, l, r, c);
  else if (l > mid)
    add(t[now].rc, l, r, c);
  else
    add(t[now].lc, l, mid, c), add(t[now].rc, mid + 1, r, c);
  update(now);
}
void st(int now, int l, int r) {
  if (t[now].l == l && t[now].r == r) {
    if (t[now].c == 0) t[now].ans += t[now].ms, t[now].ad++;
    return;
  }
  pushdown(now);
  int mid = (t[now].l + t[now].r) / 2;
  if (r <= mid)
    st(t[now].lc, l, r);
  else if (l > mid)
    st(t[now].rc, l, r);
  else
    st(t[now].lc, l, mid), st(t[now].rc, mid + 1, r);
  update(now);
}
long long query(int now, int l, int r) {
  if (t[now].l == l && r == t[now].r) return t[now].ans;
  pushdown(now);
  int mid = (t[now].l + t[now].r) / 2;
  if (r <= mid)
    return query(t[now].lc, l, r);
  else if (l > mid)
    return query(t[now].rc, l, r);
  else
    return query(t[now].lc, l, mid) + query(t[now].rc, mid + 1, r);
  update(now);
}
int main() {
  int n = read();
  for (int i = 1; i <= n; i++) a[i] = read();
  int Q = read();
  for (int i = 1; i <= Q; i++) q[i].l = read(), q[i].r = read(), q[i].id = i;
  sort(q + 1, q + Q + 1, cmp);
  int tp = 0;
  bt(1, n);
  for (int i = 1; i <= n; i++) {
    while (tpmn && a[stamn[tpmn]] > a[i])
      add(1, stamn[tpmn - 1] + 1, stamn[tpmn], a[stamn[tpmn]] - a[i]), --tpmn;
    stamn[++tpmn] = i;
    while (tpmx && a[stamx[tpmx]] < a[i])
      add(1, stamx[tpmx - 1] + 1, stamx[tpmx], a[i] - a[stamx[tpmx]]), --tpmx;
    stamx[++tpmx] = i;
    add(1, 1, n, -1);
    st(1, 1, i);
    while (tp < Q && q[tp + 1].r <= i)
      ++tp, ans[q[tp].id] = query(1, q[tp].l, q[tp].r);
  }
  for (int i = 1; i <= Q; i++) printf("%lld\n", ans[i]);
  return 0;
}
