#include <bits/stdc++.h>
using namespace std;
long long Read() {
  long long x = 0, f = 1;
  char ch = getchar();
  while (!isdigit(ch)) {
    if (ch == '-') f = -1;
    ch = getchar();
  }
  while (isdigit(ch)) {
    x = (x << 3) + (x << 1) + ch - '0';
    ch = getchar();
  }
  return x * f;
}
long long n, a[200005], q, Ans[200005], minn[200005 << 2], mcnt[200005 << 2],
    ans[200005 << 2], addv[200005 << 2], tim[200005 << 2];
struct node {
  long long l, r, id;
  bool operator<(const node &A) const { return r < A.r; }
} que[200005];
long long stk1[200005], stk2[200005], tp1, tp2;
void pushup(long long p) {
  minn[p] = min(minn[p << 1], minn[p << 1 | 1]);
  mcnt[p] = 0;
  mcnt[p] += (minn[p << 1] == minn[p]) * mcnt[p << 1];
  mcnt[p] += (minn[p << 1 | 1] == minn[p]) * mcnt[p << 1 | 1];
  ans[p] = ans[p << 1] + ans[p << 1 | 1];
}
void build(long long p, long long l, long long r) {
  if (l == r) {
    minn[p] = l, mcnt[p] = 1;
    return;
  }
  long long mid = (l + r) >> 1;
  build(p << 1, l, mid);
  build(p << 1 | 1, mid + 1, r);
  pushup(p);
}
void pusha(long long p, long long x) {
  minn[p] += x;
  addv[p] += x;
}
void pusht(long long p, long long x) {
  ans[p] += mcnt[p] * x;
  tim[p] += x;
}
void pushdpwn(long long p) {
  if (addv[p]) pusha(p << 1, addv[p]), pusha(p << 1 | 1, addv[p]), addv[p] = 0;
  if (tim[p]) {
    if (minn[p << 1] == minn[p]) pusht(p << 1, tim[p]);
    if (minn[p << 1 | 1] == minn[p]) pusht(p << 1 | 1, tim[p]);
    tim[p] = 0;
  }
}
void modify(long long p, long long l, long long r, long long nl, long long nr,
            long long val) {
  if (nl <= l && r <= nr) return pusha(p, val);
  long long mid = (l + r) >> 1;
  pushdpwn(p);
  if (nl <= mid) modify(p << 1, l, mid, nl, nr, val);
  if (mid < nr) modify(p << 1 | 1, mid + 1, r, nl, nr, val);
  pushup(p);
}
long long query(long long p, long long l, long long r, long long nl,
                long long nr) {
  if (nl <= l && r <= nr) return ans[p];
  long long mid = (l + r) >> 1, res = 0;
  pushdpwn(p);
  if (nl <= mid) res += query(p << 1, l, mid, nl, nr);
  if (mid < nr) res += query(p << 1 | 1, mid + 1, r, nl, nr);
  return res;
}
signed main() {
  n = Read();
  for (long long i = 1; i <= n; i++) a[i] = Read();
  build(1, 1, n);
  q = Read();
  for (long long i = 1; i <= q; i++)
    que[i].l = Read(), que[i].r = Read(), que[i].id = i;
  sort(que + 1, que + q + 1);
  long long cnt = 1;
  for (long long i = 1; i <= n; i++) {
    pusha(1, -1);
    while (tp1 && a[i] > a[stk1[tp1]]) {
      modify(1, 1, n, stk1[tp1 - 1] + 1, stk1[tp1], a[i] - a[stk1[tp1]]);
      --tp1;
    }
    while (tp2 && a[i] < a[stk2[tp2]]) {
      modify(1, 1, n, stk2[tp2 - 1] + 1, stk2[tp2], a[stk2[tp2]] - a[i]);
      --tp2;
    }
    stk1[++tp1] = i;
    stk2[++tp2] = i;
    pusht(1, 1);
    while (que[cnt].r == i && cnt <= q)
      Ans[que[cnt].id] = query(1, 1, n, que[cnt].l, que[cnt].r), ++cnt;
  }
  for (long long i = 1; i <= q; i++) printf("%lld\n", Ans[i]);
  return 0;
}
