#include <bits/stdc++.h>
int n, m;
struct tree {
  int l, r;
  long long minn;
  long long ans;
  int cnt;
  long long tag_min, tag_ans;
} tr[200007 << 2];
int a[200007];
struct Q {
  int l, r;
  int id;
} q[200007];
long long ans[200007];
void pushmin(int cnt, int v) {
  tr[cnt].minn += v;
  tr[cnt].tag_min += v;
  return;
}
void pushans(int cnt, int v) {
  tr[cnt].ans += tr[cnt].cnt * v;
  tr[cnt].tag_ans += v;
  return;
}
void pushup(int cnt) {
  tr[cnt].minn = std::min(tr[cnt << 1].minn, tr[cnt << 1 | 1].minn);
  tr[cnt].ans = tr[cnt << 1].ans + tr[cnt << 1 | 1].ans;
  tr[cnt].cnt = 0;
  if (tr[cnt].minn == tr[cnt << 1].minn) tr[cnt].cnt += tr[cnt << 1].cnt;
  if (tr[cnt].minn == tr[cnt << 1 | 1].minn)
    tr[cnt].cnt += tr[cnt << 1 | 1].cnt;
  return;
}
void pushdown(int cnt) {
  pushmin(cnt << 1, tr[cnt].tag_min);
  pushmin(cnt << 1 | 1, tr[cnt].tag_min);
  tr[cnt].tag_min = 0;
  if (tr[cnt].minn == tr[cnt << 1].minn) pushans(cnt << 1, tr[cnt].tag_ans);
  if (tr[cnt].minn == tr[cnt << 1 | 1].minn)
    pushans(cnt << 1 | 1, tr[cnt].tag_ans);
  tr[cnt].tag_ans = 0;
  return;
}
void build(int l, int r, int cnt) {
  tr[cnt].l = l;
  tr[cnt].r = r;
  tr[cnt].cnt = 1;
  tr[cnt].minn = l;
  if (l == r) return;
  int mid = (l + r) >> 1;
  build(l, mid, cnt << 1);
  build(mid + 1, r, cnt << 1 | 1);
  return;
}
void put(int l1, int r1, int cnt, int v) {
  int l = tr[cnt].l, r = tr[cnt].r;
  if (l != r) pushdown(cnt);
  if (l1 <= l && r <= r1) {
    pushmin(cnt, v);
    return;
  }
  int mid = (l + r) >> 1;
  if (l1 <= mid) put(l1, r1, cnt << 1, v);
  if (mid + 1 <= r1) put(l1, r1, cnt << 1 | 1, v);
  pushup(cnt);
}
long long find(int l1, int r1, int cnt) {
  int l = tr[cnt].l, r = tr[cnt].r;
  if (l != r) pushdown(cnt);
  if (l1 <= l && r <= r1) {
    return tr[cnt].ans;
  }
  int mid = (l + r) >> 1;
  long long ans = 0;
  if (l1 <= mid) ans += find(l1, r1, cnt << 1);
  if (mid + 1 <= r1) ans += find(l1, r1, cnt << 1 | 1);
  return ans;
}
int smin[200007], smax[200007];
int topmin, topmax;
bool cmp(Q x, Q y) { return x.r < y.r; }
int main() {
  scanf("%d", &n);
  build(1, n, 1);
  for (int i = 1; i <= n; ++i) scanf("%d", &a[i]);
  scanf("%d", &m);
  for (int i = 1; i <= m; ++i) {
    scanf("%d%d", &q[i].l, &q[i].r);
    q[i].id = i;
  }
  std::sort(q + 1, q + m + 1, cmp);
  int now_ = 1;
  for (int i = 1; i <= n; ++i) {
    pushmin(1, -1);
    int last = 0;
    while (topmin && a[smin[topmin]] > a[i]) {
      put(smin[topmin - 1] + 1, smin[topmin], 1, a[smin[topmin]] - a[i]);
      if (!last) last = smin[topmin];
      --topmin;
    }
    ++topmin;
    smin[topmin] = i;
    last = 0;
    while (topmax && a[smax[topmax]] < a[i]) {
      put(smax[topmax - 1] + 1, smax[topmax], 1, a[i] - a[smax[topmax]]);
      if (!last) last = smax[topmax];
      --topmax;
    }
    ++topmax;
    smax[topmax] = i;
    last = 0;
    pushans(1, 1);
    while (q[now_].r == i && now_ <= m) {
      ans[q[now_].id] = find(q[now_].l, q[now_].r, 1);
      ++now_;
    }
  }
  for (int i = 1; i <= m; ++i) {
    printf("%lld\n", ans[i]);
  }
  return 0;
}
