#include <bits/stdc++.h>
using namespace std;
const int maxn = 200010, mo = 1e9 + 7;
const double LOG = log(2);
int n, q, l, r, s[maxn], sa[maxn], rk[maxn], h[maxn], cnt, LG[maxn];
long long e[maxn], ha[maxn * 2][22];
int rmq[maxn][22], ex[22];
vector<int> T[maxn];
void Init() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) scanf("%d", s + i);
  for (int i = 1; i < n; i++) s[i] = s[i + 1] - s[i];
  s[n] = 1e9 + 1;
  for (int i = n + 1; i < n + n; i++) s[i] = -s[i - n];
  s[n + n] = -s[n];
  ex[0] = 1;
  for (int i = 1; i < 22; i++) ex[i] = ex[i - 1] << 1;
  for (int i = 1; i < maxn; i++) LG[i] = log(i) / LOG;
}
inline bool cmp(int x, int y) {
  for (int k = LG[n + n - max(x, y)]; k >= 0; k--)
    if (ha[x][k] == ha[y][k]) x += ex[k], y += ex[k];
  return s[x] == s[y] ? s[x + 1] < s[y + 1] : s[x] < s[y];
}
void Suffix_Array() {
  e[0] = 1;
  for (int i = 1; i <= n + n; i++) e[i] = (e[i - 1] * 97) % mo, ha[i][0] = s[i];
  for (int k = 1, L = 1; L <= n + n; k++, L <<= 1)
    for (int i = 1; i <= n + n; i++)
      ha[i][k] = ((long long)ha[i][k - 1] * e[L] + ha[i + L][k - 1]) % mo;
  for (int i = 1; i < n + n; i++) sa[i] = i;
  sort(sa + 1, sa + n + n, cmp);
  for (int i = 1; i < n + n; i++) rk[sa[i]] = i;
  for (int i = 1; i < n + n; i++) {
    if (rk[i] == 1) continue;
    int len = max(0, h[rk[i - 1]] - 1);
    while (s[i + len] == s[sa[rk[i] - 1] + len]) len++;
    h[rk[i]] = len;
  }
}
inline int rmq_query(int l, int r) {
  int e = log(r - l + 1) / LOG;
  if (ex[e] > r) e--;
  return min(rmq[l][e], rmq[r - ex[e] + 1][e]);
}
void RMQ() {
  for (int i = 1; i < n + n; i++) rmq[i][0] = h[i];
  for (int e = 1, range = 1; range < n + n; e++, range <<= 1)
    for (int i = 1; i < n + n - range; i++)
      rmq[i][e] = min(rmq[i][e - 1], rmq[i + range][e - 1]);
}
inline int ask(int x, int val) {
  int ret = 0;
  for (; x > 0; x -= x & -x)
    ret += lower_bound(T[x].begin(), T[x].end(), val) - T[x].begin();
  return ret;
}
void Answer() {
  for (int i = 1; i < n + n; i++)
    for (int j = i; j > i - (i & -i); j--)
      if (sa[j] > n) T[i].push_back(sa[j] - n);
  for (int i = 1; i < n + n; i++) sort(T[i].begin(), T[i].end());
  for (int i = 1; i < n + n; i++) s[i] = s[i - 1] + (sa[i] > n);
  for (scanf("%d", &q); q--;) {
    scanf("%d%d", &l, &r);
    r--;
    if (l > r) {
      printf("%d\n", n - 1);
      continue;
    }
    if ((r - l + 2) * 2 > n) {
      printf("0\n");
      continue;
    }
    int vl = 0, vr = rk[l] - 1, pos = rk[l], len = r - l + 1, rl, rr;
    while (vl < vr) {
      int m = (vl + vr + 1) >> 1;
      if (rmq_query(pos - m + 1, pos) >= len)
        vl = m;
      else
        vr = m - 1;
    }
    rl = pos - vl;
    vl = 0;
    vr = n + n - rk[l] - 1;
    while (vl < vr) {
      int m = (vl + vr + 1) >> 1;
      if (rmq_query(pos + 1, pos + m) >= len)
        vl = m;
      else
        vr = m - 1;
    }
    rr = pos + vl;
    printf("%d\n", ask(rr, l - len) - ask(rl - 1, l - len) + s[rr] - s[rl - 1] -
                       ask(rr, r + 2) + ask(rl - 1, r + 2));
  }
}
int main() {
  Init();
  Suffix_Array();
  RMQ();
  Answer();
}
