#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5;
const long long base = 1610612741;
const long long mod = 1311322777;
long long diff[N], rev[N], hasH[N][2], mu[N], b[N];
int n, q, a[N], l, r, ans[N], fen[N];
pair<int, int> val[N];
vector<pair<pair<int, int>, int> > queryL[N], queryR[N];
void update(int id, int val) {
  for (int i = id; i <= n; i += (i & -i)) {
    fen[i] += val;
  }
}
int get(int id) {
  int sum = 0;
  for (int i = id; i >= 1; i -= (i & -i)) {
    sum += fen[i];
  }
  return sum;
}
long long get_hash(int l, int r, int type) {
  return (hasH[r][type] - hasH[l - 1][type] * mu[r - l + 1] + mod * mod) % mod;
}
bool cmp(int a, int b) {
  int l = 1, r = min(n - a + 1, n - b + 1);
  while (l < r) {
    int mid = (l + r) >> 1;
    if (get_hash(a, a + mid - 1, 0) == get_hash(b, b + mid - 1, 0)) {
      l = mid + 1;
    } else {
      r = mid;
    }
  }
  if (get_hash(a, a + l - 1, 0) == get_hash(b, b + l - 1, 0)) {
    return a > b;
  } else {
    return diff[a + l - 1] < diff[b + l - 1];
  }
}
int searchL(int id, int len) {
  int l = 1, r = n;
  while (l < r) {
    int mid = (l + r) >> 1;
    int lef = 1, rig = min(n - id + 1, n - a[mid] + 1);
    while (lef < rig) {
      int md = (lef + rig) >> 1;
      if (get_hash(a[mid], a[mid] + md - 1, 0) ==
          get_hash(id, id + md - 1, 1)) {
        lef = md + 1;
      } else {
        rig = md;
      }
    }
    if (diff[a[mid] + lef - 1] == rev[id + lef - 1]) {
      if (lef < len) {
        l = mid + 1;
      } else {
        r = mid;
      }
    } else {
      if (lef <= len && diff[a[mid] + lef - 1] < rev[id + lef - 1]) {
        l = mid + 1;
      } else {
        r = mid;
      }
    }
  }
  int tmp = min(n - id + 1, n - a[l] + 1);
  if (tmp >= len &&
      get_hash(a[l], a[l] + len - 1, 0) == get_hash(id, id + len - 1, 1)) {
    return l;
  } else {
    return -1;
  }
}
int searchR(int id, int len) {
  int l = 1, r = n;
  while (l + 1 < r) {
    int mid = (l + r) >> 1;
    int lef = 1, rig = min(n - id + 1, n - a[mid] + 1);
    while (lef < rig) {
      int md = (lef + rig) >> 1;
      if (get_hash(a[mid], a[mid] + md - 1, 0) ==
          get_hash(id, id + md - 1, 1)) {
        lef = md + 1;
      } else {
        rig = md;
      }
    }
    if (lef <= len && diff[a[mid] + lef - 1] > rev[id + lef - 1]) {
      r = mid;
    } else {
      l = mid;
    }
  }
  int tmp = min(n - id + 1, n - a[r] + 1);
  if (tmp >= len &&
      get_hash(a[r], a[r] + len - 1, 0) == get_hash(id, id + len - 1, 1)) {
    return r;
  } else {
    int tmp = min(n - id + 1, n - a[l] + 1);
    if (tmp >= len &&
        get_hash(a[l], a[l] + len - 1, 0) == get_hash(id, id + len - 1, 1)) {
      return l;
    } else {
      return -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];
    diff[i] = a[i] - a[i - 1];
    rev[i] = -diff[i];
    b[i] = a[i];
  }
  mu[0] = 1;
  for (int i = 1; i <= n; i++) {
    hasH[i][0] = (hasH[i - 1][0] * base + (diff[i] + mod * mod) % mod) % mod;
    hasH[i][1] = (hasH[i - 1][1] * base + (rev[i] + mod * mod) % mod) % mod;
    mu[i] = (mu[i - 1] * base) % mod;
    a[i] = i;
  }
  sort(a + 1, a + 1 + n, cmp);
  for (int i = 1; i <= n; i++) {
    val[i].first = a[i];
    val[i].second = i;
  }
  cin >> q;
  for (int i = 1; i <= q; i++) {
    cin >> l >> r;
    if (l == r) {
      ans[i] = n - 1;
    } else {
      l++;
      int len = r - l + 1;
      int lef = searchL(l, len);
      int rig = searchR(l, len);
      if (lef == -1 || rig == -1) {
        ans[i] = 0;
      } else {
        if (l - len - 1 >= 2) {
          queryL[l - len - 1].push_back({{lef, rig}, i});
        }
        if (r + 2 <= n) {
          queryR[r + 2].push_back({{lef, rig}, i});
        }
      }
    }
  }
  sort(val + 1, val + 1 + n);
  for (int i = 1; i <= n; i++) {
    if (val[i].first > 1) {
      update(val[i].second, 1);
    }
    for (auto tmp : queryL[val[i].first]) {
      int L = tmp.first.first, R = tmp.first.second, id = tmp.second;
      ans[id] += get(R) - get(L - 1);
    }
  }
  for (int i = 1; i <= n; i++) fen[i] = 0;
  for (int i = n; i >= 1; i--) {
    if (val[i].first > 1) {
      update(val[i].second, 1);
    }
    for (auto tmp : queryR[val[i].first]) {
      int L = tmp.first.first, R = tmp.first.second, id = tmp.second;
      ans[id] += get(R) - get(L - 1);
    }
  }
  for (int i = 1; i <= q; i++) {
    cout << ans[i] << "\n";
  }
}
