#include <bits/stdc++.h>
using namespace std;
long long n, m, x, y, t = 1, top[2];
const long long N = 120010;
long long a[N], mn[N << 2], tr[N << 2], js[N << 2], lz[N << 2], ti[N << 2],
    zhan[2][N], ans[N];
struct wen {
  long long l, r, id;
} q[N];
long long my(wen a, wen b) { return a.r < b.r; };
void pushup(long long k) {
  mn[k] = min(mn[(k << 1)], mn[((k << 1) | 1)]);
  tr[k] = (mn[k] == mn[(k << 1)] ? tr[(k << 1)] : 0) +
          (mn[k] == mn[((k << 1) | 1)] ? tr[((k << 1) | 1)] : 0);
  js[k] = js[(k << 1)] + js[((k << 1) | 1)];
}
void build(long long k, long long l, long long r) {
  if (l == r) {
    mn[k] = l;
    tr[k] = 1;
    return;
  }
  long long mid = (l + r) >> 1;
  build((k << 1), l, mid);
  build(((k << 1) | 1), mid + 1, r);
  pushup(k);
}
void pushdown(long long k) {
  if (lz[k])
    lz[(k << 1)] += lz[k], mn[(k << 1)] += lz[k], lz[((k << 1) | 1)] += lz[k],
        mn[((k << 1) | 1)] += lz[k], lz[k] = 0;
  if (ti[k]) {
    if (mn[k] == mn[(k << 1)])
      js[(k << 1)] += tr[(k << 1)] * ti[k], ti[(k << 1)] += ti[k];
    if (mn[k] == mn[((k << 1) | 1)])
      js[((k << 1) | 1)] += tr[((k << 1) | 1)] * ti[k],
          ti[((k << 1) | 1)] += ti[k];
    ti[k] = 0;
  }
}
void change(long long k, long long l, long long r, long long x, long long y,
            long long v) {
  if (x <= l && r <= y) {
    lz[k] += v;
    mn[k] += v;
    return;
  }
  pushdown(k);
  long long mid = (l + r) >> 1;
  if (x <= mid) change((k << 1), l, mid, x, y, v);
  if (mid + 1 <= y) change(((k << 1) | 1), mid + 1, r, x, y, v);
  pushup(k);
}
long long ask(long long k, long long l, long long r, long long x, long long y) {
  if (x <= l && r <= y) return js[k];
  pushdown(k);
  long long mid = (l + r) >> 1, res = 0;
  if (x <= mid) res += ask((k << 1), l, mid, x, y);
  if (mid + 1 <= y) res += ask(((k << 1) | 1), mid + 1, r, x, y);
  return res;
}
signed main() {
  cin >> n;
  for (long long i = 1; i <= n; ++i) scanf("%lld", &a[i]);
  build(1, 1, n);
  cin >> m;
  for (long long i = 1; i <= m; ++i)
    scanf("%lld%lld", &q[i].l, &q[i].r), q[i].id = i;
  sort(q + 1, q + 1 + m, my);
  for (long long i = 1, now; i <= n; ++i) {
    now = i;
    while (top[0] && a[zhan[0][top[0]]] > a[i])
      change(1, 1, n, zhan[0][top[0] - 1] + 1, now - 1,
             a[zhan[0][top[0]]] - a[i]),
          now = zhan[0][top[0]--];
    while (top[1] && a[zhan[1][top[1]]] < a[i])
      change(1, 1, n, zhan[1][top[1] - 1] + 1, now - 1,
             a[i] - a[zhan[1][top[1]]]),
          now = zhan[1][top[1]--];
    zhan[0][++top[0]] = zhan[1][++top[1]] = i;
    js[1] += tr[1];
    ++ti[1];
    while (t <= m && q[t].r == i)
      ans[q[t].id] = ask(1, 1, n, q[t].l, q[t].r), ++t;
  }
  for (long long i = 1; i <= m; ++i) printf("%lld\n", ans[i]);
  return 0;
}
