#include <bits/stdc++.h>
using namespace std;
const long long N = 2e5 + 5;
const long long A = 1610612741;
const long long B = 1311322777;
long long a[N], h[N][3], dif[N], pw[N], tree[N], ans[N], rev[N];
pair<long long, long long> val[N];
long long n;
vector<pair<pair<long long, long long>, long long> > queL[N], queR[N];
struct rng {
  long long l, r;
} s[N];
long long sum(long long k) {
  long long s = 0;
  while (k >= 1) {
    s += tree[k];
    k -= k & -k;
  }
  return s;
}
void add(long long k, long long x) {
  while (k <= n) {
    tree[k] += x;
    k += k & -k;
  }
}
long long get(long long l, long long r, long long type) {
  return (h[r][type] - h[l - 1][type] * pw[r - l + 1] + B * B) % B;
}
long long find(rng p, rng q, long long flag) {
  long long u = p.l, v = q.l;
  long long L = 1, R = min(p.r - p.l + 1, q.r - q.l + 1);
  while (L < R) {
    long long mid = (L + R) >> 1;
    if (get(u, u + mid - 1, 0) == get(v, v + mid - 1, 0 ^ flag))
      L = mid + 1;
    else
      R = mid;
  }
  return L;
}
bool cmp(rng p, rng q) {
  long long L = find(p, q, 0ll);
  long long u = p.l, v = q.l;
  if (get(u, u + L - 1, 0) == get(v, v + L - 1, 0))
    return u > v;
  else
    return dif[u + L - 1] < dif[v + L - 1];
}
long long bsl(rng q) {
  long long L = 1, R = n, len = q.r - q.l + 1, u = q.l, v;
  while (L < R) {
    long long mid = (L + R) >> 1, v = s[mid].l;
    long long lef = find(s[mid], q, 1ll);
    if (dif[v + lef - 1] == rev[u + lef - 1]) {
      if (lef < len)
        L = mid + 1;
      else
        R = mid;
    } else {
      if (lef <= len && dif[v + lef - 1] < rev[u + lef - 1])
        L = mid + 1;
      else
        R = mid;
    }
  }
  v = s[L].l;
  long long tmp = min(n - u + 1, n - v + 1);
  if (tmp >= len && get(v, v + len - 1, 0) == get(u, u + len - 1, 1))
    return L;
  else
    return -1;
}
long long bsr(rng q) {
  long long L = 1, R = n, len = q.r - q.l + 1, u = q.l, v;
  while (L + 1 < R) {
    long long mid = (L + R) >> 1, v = s[mid].l;
    long long lef = find(s[mid], q, 1ll);
    if (lef <= len && dif[v + lef - 1] > rev[u + lef - 1])
      R = mid;
    else
      L = mid;
  }
  v = s[R].l;
  long long tmp = min(n - u + 1, n - v + 1);
  if (tmp >= len && get(v, v + len - 1, 0) == get(u, u + len - 1, 1))
    return R;
  else {
    v = s[L].l;
    tmp = min(n - u + 1, n - v + 1);
    if (tmp >= len && get(v, v + len - 1, 0) == get(u, u + len - 1, 1))
      return L;
    else
      return -1;
  }
}
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cin >> n;
  for (long long i = 1; i <= n; i++) {
    cin >> a[i];
  }
  pw[0] = 1;
  for (long long i = 1; i <= n; i++) {
    dif[i] = a[i] - a[i - 1];
    rev[i] = -dif[i];
    h[i][0] = (h[i - 1][0] * A + dif[i]) % B;
    h[i][1] = (h[i - 1][1] * A + rev[i]) % B;
    pw[i] = (pw[i - 1] * A) % B;
    s[i].l = i;
    s[i].r = n;
  }
  long long q;
  cin >> q;
  sort(s + 1, s + 1 + n, cmp);
  for (long long i = 1; i <= n; i++) {
    val[i].first = s[i].l;
    val[i].second = i;
  }
  for (long long i = 1; i <= q; i++) {
    long long l, r;
    cin >> l >> r;
    if (l == r) {
      ans[i] = n - 1;
    } else {
      l++;
      long long len = r - l + 1;
      rng p = {l, r};
      long long lef = bsl(p);
      long long rig = bsr(p);
      if (lef == -1 || rig == -1)
        ans[i] = 0;
      else {
        if (l - len - 1 >= 2) queL[l - len - 1].push_back({{lef, rig}, i});
        if (r + 2 <= n) queR[r + 2].push_back({{lef, rig}, i});
      }
    }
  }
  sort(val + 1, val + 1 + n);
  for (long long i = 1; i <= n; i++) {
    if (val[i].first > 1) {
      add(val[i].second, 1);
    }
    for (auto tmp : queL[val[i].first]) {
      long long L = tmp.first.first, R = tmp.first.second, id = tmp.second;
      ans[id] += sum(R) - sum(L - 1);
    }
  }
  memset(tree, 0, sizeof(tree));
  for (long long i = n; i >= 1; i--) {
    if (val[i].first > 1) {
      add(val[i].second, 1);
    }
    for (auto tmp : queR[val[i].first]) {
      long long L = tmp.first.first, R = tmp.first.second, id = tmp.second;
      ans[id] += sum(R) - sum(L - 1);
    }
  }
  for (long long i = 1; i <= q; i++) {
    cout << ans[i] << '\n';
  }
}
