#include <bits/stdc++.h>
using namespace std;
const int N = 4e5 + 10;
int n, cnt;
vector<int> to[N];
int v[N], pos[N], dep[N], f[N][23], dfn[N][2], ans[N];
struct SAM {
  int tot, tail;
  map<int, int> son[N];
  int fa[N], maxlen[N];
  SAM() { tot = tail = 1; }
  inline void copy(int p, int q) { fa[p] = fa[q], son[p] = son[q]; }
  inline void ins(int ch) {
    int p = tail, now = tail = ++tot;
    maxlen[now] = maxlen[p] + 1;
    for (; p && son[p].find(ch) == son[p].end(); p = fa[p]) son[p][ch] = now;
    if (!p) {
      fa[now] = 1;
      return;
    }
    int cur1 = son[p][ch], cur2;
    if (maxlen[cur1] == maxlen[p] + 1) {
      fa[now] = cur1;
      return;
    }
    copy(cur2 = ++tot, cur1);
    maxlen[cur2] = maxlen[p] + 1;
    for (; p && son[p][ch] == cur1; p = fa[p]) son[p][ch] = cur2;
    fa[now] = fa[cur1] = cur2;
  }
} t;
inline void dfs(int p) {
  dfn[p][0] = ++cnt;
  for (int q : to[p]) {
    dep[q] = dep[p] + 1, f[q][0] = p;
    for (int j = 1; j <= log2(dep[q]); j++) f[q][j] = f[f[q][j - 1]][j - 1];
    dfs(q);
  }
  dfn[p][1] = cnt;
}
inline int jump(int x, int lim) {
  for (int i = log2(dep[x]); i >= 0; i--)
    if (t.maxlen[f[x][i]] >= lim) x = f[x][i];
  return x;
}
struct ask {
  int type, x, y;
} que[N * 6];
inline bool cmp(ask a, ask b) {
  if (a.x != b.x) return a.x < b.x;
  if (a.y != b.y) return a.y < b.y;
  return abs(a.type) < abs(b.type);
}
struct tree {
  int val[N];
  inline int lowbit(int x) { return x & (-x); }
  inline void change(int x) {
    for (int i = x; i <= n; i += lowbit(i)) val[i]++;
  }
  inline int query(int x) {
    int res = 0;
    for (int i = x; i >= 1; i -= lowbit(i)) res += val[i];
    return res;
  }
} rt;
int main() {
  int T, a, b, idx = 0;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) scanf("%d", &v[i]);
  for (int i = n; i >= 1; i--) v[i] -= v[i - 1];
  for (int i = 1; i <= n; i++) v[i + n] = -v[i];
  for (int i = 1; i <= 2 * n; i++) t.ins(v[i]), pos[i] = t.tail;
  for (int i = 1; i <= t.tot; i++) to[t.fa[i]].push_back(i);
  dep[1] = 1, dfs(1);
  scanf("%d", &T);
  t.maxlen[0] = -1;
  for (int i = 1; i <= T; i++) {
    scanf("%d%d", &a, &b);
    if (a == b) {
      ans[i] = n - 1;
      continue;
    }
    int len = b - a + 1, pla = jump(pos[b + n], len - 1);
    if (b + len <= n)
      que[++idx] = (ask){i, dfn[pla][1], n},
      que[++idx] = (ask){-i, dfn[pla][1], b + len - 1};
    if (a >= 2) que[++idx] = (ask){i, dfn[pla][1], a - 1};
    if (b + len <= n)
      que[++idx] = (ask){-i, dfn[pla][0] - 1, n},
      que[++idx] = (ask){i, dfn[pla][0] - 1, b + len - 1};
    if (a >= 2) que[++idx] = (ask){-i, dfn[pla][0] - 1, a - 1};
  }
  for (int i = 2; i <= n; i++) que[++idx] = (ask){0, dfn[pos[i]][0], i};
  sort(que + 1, que + idx + 1, cmp);
  for (int i = 1; i <= idx; i++) {
    if (que[i].type == 0) rt.change(que[i].y);
    if (que[i].type > 0) ans[que[i].type] += rt.query(que[i].y);
    if (que[i].type < 0) ans[-que[i].type] -= rt.query(que[i].y);
  }
  for (int i = 1; i <= T; i++) printf("%d\n", ans[i]);
  return 0;
}
