#include <bits/stdc++.h>
using namespace std;
template <typename T1, typename T2>
inline T1 max(T1 a, T2 b) {
  return a < b ? b : a;
}
template <typename T1, typename T2>
inline T1 min(T1 a, T2 b) {
  return a < b ? a : b;
}
const char lf = '\n';
namespace ae86 {
const int bufl = 1 << 15;
char buf[bufl], *s = buf, *t = buf;
inline int fetch() {
  if (s == t) {
    t = (s = buf) + fread(buf, 1, bufl, stdin);
    if (s == t) return EOF;
  }
  return *s++;
}
inline int ty() {
  int a = 0;
  int b = 1, c = fetch();
  while (!isdigit(c)) b ^= c == '-', c = fetch();
  while (isdigit(c)) a = a * 10 + c - 48, c = fetch();
  return b ? a : -a;
}
}  // namespace ae86
using ae86::ty;
const int _ = 120007;
struct sgt {
  static const int _t = _ << 2;
  int mi[_t], tadd[_t];
  long long cnt[_t], tcnt[_t], ans[_t];
  void up(int x) {
    mi[x] = min(mi[(x << 1)], mi[(x << 1 | 1)]), cnt[x] = 0;
    if (mi[x] == mi[(x << 1)]) cnt[x] += cnt[(x << 1)];
    if (mi[x] == mi[(x << 1 | 1)]) cnt[x] += cnt[(x << 1 | 1)];
    ans[x] = ans[(x << 1)] + ans[(x << 1 | 1)];
  }
  void putadd(int x, int d) { mi[x] += d, tadd[x] += d; }
  void putcnt(int x, long long d) { ans[x] += cnt[x] * d, tcnt[x] += d; }
  void dwn(int x) {
    if (tadd[x])
      putadd((x << 1), tadd[x]), putadd((x << 1 | 1), tadd[x]), tadd[x] = 0;
    if (tcnt[x]) {
      if (mi[x] == mi[(x << 1)]) putcnt((x << 1), tcnt[x]);
      if (mi[x] == mi[(x << 1 | 1)]) putcnt((x << 1 | 1), tcnt[x]);
      tcnt[x] = 0;
    }
  }
  void plant(int x, int l, int r) {
    mi[x] = l, cnt[x] = 1, ans[x] = 0, tadd[x] = tcnt[x] = 0;
    if (l == r) return;
    plant((x << 1), l, ((l + r) >> 1)),
        plant((x << 1 | 1), ((l + r) >> 1) + 1, r);
  }
  void change(int x, int l, int r, int tl, int tr, int d) {
    if (tl <= l && r <= tr) {
      putadd(x, d);
      return;
    }
    dwn(x);
    if (tl <= ((l + r) >> 1)) change((x << 1), l, ((l + r) >> 1), tl, tr, d);
    if (tr > ((l + r) >> 1))
      change((x << 1 | 1), ((l + r) >> 1) + 1, r, tl, tr, d);
    up(x);
  }
  long long finder(int x, int l, int r, int tl, int tr) {
    if (tl <= l && r <= tr) return ans[x];
    dwn(x);
    if (tr <= ((l + r) >> 1))
      return finder((x << 1), l, ((l + r) >> 1), tl, tr);
    if (tl > ((l + r) >> 1))
      return finder((x << 1 | 1), ((l + r) >> 1) + 1, r, tl, tr);
    return finder((x << 1), l, ((l + r) >> 1), tl, tr) +
           finder((x << 1 | 1), ((l + r) >> 1) + 1, r, tl, tr);
  }
} t;
struct cxk {
  int id, l, r;
  cxk(int id_ = 0, int l_ = 0, int r_ = 0) { id = id_, l = l_, r = r_; }
  friend int operator<(cxk a, cxk b) { return a.r < b.r; }
} q[_];
int n, qn, val[_];
int smi[_], smx[_], tmi, tmx;
long long ans[_] = {0};
int main() {
  ios::sync_with_stdio(0), cout.tie(nullptr);
  n = ty();
  for (int i = 1; i <= n; i++) val[i] = ty();
  qn = ty();
  for (int i = 1, a, b; i <= qn; i++) a = ty(), b = ty(), q[i] = cxk(i, a, b);
  sort(q + 1, q + qn + 1);
  t.plant(1, 1, n);
  smi[0] = smx[0] = 0, tmi = tmx = 0;
  for (int i = 1, now = 1; i <= n; i++) {
    t.putadd(1, -1);
    while (tmx > 0 && val[smx[tmx]] < val[i])
      t.change(1, 1, n, smx[tmx - 1] + 1, smx[tmx], val[i] - val[smx[tmx]]),
          tmx--;
    smx[++tmx] = i;
    while (tmi > 0 && val[smi[tmi]] > val[i])
      t.change(1, 1, n, smi[tmi - 1] + 1, smi[tmi], val[smi[tmi]] - val[i]),
          tmi--;
    smi[++tmi] = i;
    t.putcnt(1, 1);
    while (now <= qn && q[now].r == i)
      ans[q[now].id] = t.finder(1, 1, n, q[now].l, q[now].r), now++;
  }
  for (int i = 1; i <= qn; i++) cout << ans[i] << lf;
  return 0;
}
