#include <bits/stdc++.h>
using namespace std;
template <typename T>
inline T read() {
  register T sum = 0;
  register char cc = getchar();
  int sym = 1;
  while (cc != '-' && (cc > '9' || cc < '0')) cc = getchar();
  if (cc == '-') sym = -1, cc = getchar();
  sum = sum * 10 + cc - '0';
  cc = getchar();
  while (cc >= '0' && cc <= '9') sum = sum * 10 + cc - '0', cc = getchar();
  return sym * sum;
}
template <typename T>
inline T read(T &a) {
  a = read<T>();
  return a;
}
template <typename T, typename... Others>
inline void read(T &a, Others &...b) {
  a = read(a);
  read(b...);
}
struct Query {
  int l, r, p;
  bool operator<(const Query &o) const { return r < o.r; }
} Q[120010];
int n, m, tp1, tp2, a[120010], sta1[120010], sta2[120010];
int mn[120010 << 2], cnt[120010 << 2], tag1[120010 << 2], tag2[120010 << 2];
long long ans[120010 << 2], out[120010];
void Effect1(int k, int v) {
  mn[k] += v;
  tag1[k] += v;
}
void Effect2(int k, int v) {
  ans[k] += 1ll * cnt[k] * v;
  tag2[k] += v;
}
void down(int k) {
  if (tag1[k]) {
    Effect1(k << 1, tag1[k]);
    Effect1(k << 1 | 1, tag1[k]);
    tag1[k] = 0;
  }
  if (tag2[k]) {
    if (mn[k << 1] == mn[k]) Effect2(k << 1, tag2[k]);
    if (mn[k << 1 | 1] == mn[k]) Effect2(k << 1 | 1, tag2[k]);
    tag2[k] = 0;
  }
}
void build(int k, int l, int r) {
  mn[k] = l;
  cnt[k] = 1;
  if (l == r) return;
  int mid = (l + r) >> 1;
  build(k << 1, l, mid);
  build(k << 1 | 1, mid + 1, r);
}
void change(int k, int l, int r, int x, int y, int v) {
  if (l >= x && r <= y) {
    Effect1(k, v);
    return;
  }
  down(k);
  int mid = (l + r) >> 1;
  if (x <= mid) change(k << 1, l, mid, x, y, v);
  if (y > mid) change(k << 1 | 1, mid + 1, r, x, y, v);
  mn[k] = min(mn[k << 1], mn[k << 1 | 1]);
  cnt[k] = 0;
  if (mn[k << 1] == mn[k]) cnt[k] += cnt[k << 1];
  if (mn[k << 1 | 1] == mn[k]) cnt[k] += cnt[k << 1 | 1];
  ans[k] = ans[k << 1] + ans[k << 1 | 1];
}
long long query(int k, int l, int r, int x, int y) {
  if (l >= x && r <= y) return ans[k];
  down(k);
  int mid = (l + r) >> 1;
  long long res = 0;
  if (x <= mid) res += query(k << 1, l, mid, x, y);
  if (y > mid) res += query(k << 1 | 1, mid + 1, r, x, y);
  return res;
}
int main() {
  read(n);
  for (int i = 1; i <= n; i++) read(a[i]);
  read(m);
  for (int i = 1; i <= m; i++) {
    read(Q[i].l, Q[i].r);
    Q[i].p = i;
  }
  sort(Q + 1, Q + 1 + m);
  build(1, 1, n);
  for (int i = 1, j = 1; i <= n; i++) {
    Effect1(1, -1);
    while (tp1 && a[sta1[tp1]] < a[i]) {
      change(1, 1, n, sta1[tp1 - 1] + 1, sta1[tp1], a[i] - a[sta1[tp1]]);
      tp1 -= 1;
    }
    sta1[++tp1] = i;
    while (tp2 && a[sta2[tp2]] > a[i]) {
      change(1, 1, n, sta2[tp2 - 1] + 1, sta2[tp2], a[sta2[tp2]] - a[i]);
      tp2 -= 1;
    }
    sta2[++tp2] = i;
    Effect2(1, 1);
    while (j <= m && Q[j].r == i) {
      out[Q[j].p] = query(1, 1, n, Q[j].l, Q[j].r);
      j += 1;
    }
  }
  for (int i = 1; i <= m; i++) printf("%lld\n", out[i]);
  return 0;
}
