#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize("O2,no-stack-protector,unroll-loops,fast-math")
const long long maxn = 12e4 + 10, maxm = 2e3 + 10, lg = 18, mod = 1e9 + 7,
                inf = 1e18;
struct node {
  long long mn = 0, cnt = 0, laz = 0, ans = 0, alaz = 0;
} seg[maxn << 2];
long long n, q, a[maxn], ans[maxn];
vector<pair<long long, long long> > qu[maxn];
void bild(long long s = 0, long long t = n + 1, long long v = 1) {
  seg[v].mn = s, seg[v].cnt = 1;
  if (t - s == 1) return;
  bild(s, ((s + t) >> 1), (v << 1)), bild(((s + t) >> 1), t, ((v << 1) ^ 1));
}
void shift(long long v, long long c) {
  seg[c].mn += seg[v].laz;
  seg[c].laz += seg[v].laz;
  if (seg[v].mn == seg[c].mn) {
    seg[c].ans += seg[v].alaz * seg[c].cnt;
    seg[c].alaz += seg[v].alaz;
  }
}
void add(long long l, long long r, long long x, long long s = 0,
         long long t = n + 1, long long v = 1) {
  if (l >= t || r <= s) return;
  if (l <= s && r >= t) {
    seg[v].mn += x;
    seg[v].laz += x;
    return;
  }
  shift(v, (v << 1)), shift(v, ((v << 1) ^ 1));
  seg[v].laz = seg[v].alaz = 0;
  add(l, r, x, s, ((s + t) >> 1), (v << 1));
  add(l, r, x, ((s + t) >> 1), t, ((v << 1) ^ 1));
  seg[v].cnt = 0;
  if (seg[(v << 1)].mn <= seg[((v << 1) ^ 1)].mn)
    seg[v].mn = seg[(v << 1)].mn, seg[v].cnt += seg[(v << 1)].cnt;
  if (seg[(v << 1)].mn >= seg[((v << 1) ^ 1)].mn)
    seg[v].mn = seg[((v << 1) ^ 1)].mn, seg[v].cnt += seg[((v << 1) ^ 1)].cnt;
  seg[v].ans = seg[(v << 1)].ans + seg[((v << 1) ^ 1)].ans;
}
long long get(long long l, long long r, long long s = 0, long long t = n + 1,
              long long v = 1) {
  if (l >= t || r <= s) return 0;
  if (l <= s && r >= t) return seg[v].ans;
  shift(v, (v << 1)), shift(v, ((v << 1) ^ 1));
  seg[v].laz = seg[v].alaz = 0;
  return get(l, r, s, ((s + t) >> 1), (v << 1)) +
         get(l, r, ((s + t) >> 1), t, ((v << 1) ^ 1));
}
int main() {
  ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  cin >> n;
  for (int i = 1; i <= n; i++) cin >> a[i];
  cin >> q;
  for (int i = 0; i < q; i++) {
    long long l, r;
    cin >> l >> r;
    qu[r].push_back({l, i});
  }
  bild();
  vector<long long> maxi, mini;
  maxi.push_back(0), mini.push_back(0);
  for (int i = 1; i <= n; i++) {
    while ((long long)(maxi.size()) > 1) {
      long long xx = maxi.back();
      if (a[xx] > a[i]) break;
      maxi.pop_back();
      add(maxi.back() + 1, xx + 1, a[i] - a[xx]);
    }
    maxi.push_back(i);
    while ((long long)(mini.size()) > 1) {
      long long xx = mini.back();
      if (a[xx] < a[i]) break;
      mini.pop_back();
      add(mini.back() + 1, xx + 1, a[xx] - a[i]);
    }
    mini.push_back(i);
    add(1, n + 1, -1);
    seg[1].ans += seg[1].cnt, seg[1].alaz++;
    for (auto x : qu[i]) ans[x.second] = get(x.first, i + 1);
  }
  for (int i = 0; i < q; i++) cout << ans[i] << '\n';
  return 0;
}
