Problem: John Doe has a crooked fence, consisting of n rectangular planks, lined up from the left to the right: the plank that goes i-th (1 ≤ i ≤ n) (from left to right) has width 1 and height hi. We will assume that the plank that goes i-th (1 ≤ i ≤ n) (from left to right) has index i.

A piece of the fence from l to r (1 ≤ l ≤ r ≤ n) is a sequence of planks of wood with indices from l to r inclusive, that is, planks with indices l, l + 1, ..., r. The width of the piece of the fence from l to r is value r - l + 1.

Two pieces of the fence from l1 to r1 and from l2 to r2 are called matching, if the following conditions hold:

  * the pieces do not intersect, that is, there isn't a single plank, such that it occurs in both pieces of the fence; 
  * the pieces are of the same width; 
  * for all i (0 ≤ i ≤ r1 - l1) the following condition holds: hl1 + i + hl2 + i = hl1 + hl2. 



John chose a few pieces of the fence and now wants to know how many distinct matching pieces are for each of them. Two pieces of the fence are distinct if there is a plank, which belongs to one of them and does not belong to the other one.

Input

The first line contains integer n (1 ≤ n ≤ 105) — the number of wood planks in the fence. The second line contains n space-separated integers h1, h2, ..., hn (1 ≤ hi ≤ 109) — the heights of fence planks.

The third line contains integer q (1 ≤ q ≤ 105) — the number of queries. Next q lines contain two space-separated integers li and ri (1 ≤ li ≤ ri ≤ n) — the boundaries of the i-th piece of the fence.

Output

For each query on a single line print a single integer — the number of pieces of the fence that match the given one. Print the answers to the queries in the order, in which the queries are given in the input.

Examples

Input

10
1 2 2 1 100 99 99 100 100 100
6
1 4
1 2
3 4
1 5
9 10
10 10


Output

1
2
2
0
2
9

Solution (in CPP):

Solution:
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 100010;
const int INF = 0x3f3f3f3f;
namespace CrmTree {
const int MAXD = MAXN * 2 * 20;
int len;
int lc[MAXD], rc[MAXD], sum[MAXD], dtot;
int roots[MAXN * 2], tot;
int insp;
void insert(int &u, int lb, int rb, int r) {
  if (lb > insp || rb < insp) {
    u = r;
    return;
  }
  if (!u) u = ++dtot, sum[u] = sum[r];
  sum[u]++;
  if (lb == rb) return;
  int mid = (lb + rb) >> 1;
  insert(lc[u], lb, mid, lc[r]), insert(rc[u], mid + 1, rb, rc[r]);
}
int ql, qr;
int query(int u, int lb, int rb) {
  if (!u || lb > qr || rb < ql) return 0;
  if (ql <= lb && rb <= qr) return sum[u];
  int mid = (lb + rb) >> 1;
  return query(lc[u], lb, mid) + query(rc[u], mid + 1, rb);
}
inline void handleInsert(int pos) {
  insp = pos;
  tot++;
  insert(roots[tot], 0, len, roots[tot - 1]);
}
inline void passOn() {
  tot++;
  roots[tot] = roots[tot - 1];
}
int queryRect(int a, int b, int c, int d) {
  ql = c, qr = d;
  if (a > b || c > d) return 0;
  return query(roots[b], 0, len) - query(roots[a - 1], 0, len);
}
}  // namespace CrmTree
namespace SuffixArray {
int buk[MAXN * 2];
int mem[2][MAXN * 2];
void suffixArray(int *A, int len, int chr, int *sa) {
  int *x = mem[0], *y = mem[1];
  memset(buk, 0, sizeof buk);
  for (int i = 0; i < len; i++) buk[x[i] = A[i]]++;
  for (int i = 1; i <= chr; i++) buk[i] += buk[i - 1];
  for (int i = len - 1; i >= 0; i--) sa[--buk[x[i]]] = i;
  for (int lvl = 1, p = 0; chr + 1 < len; chr = p, lvl <<= 1) {
    p = 0;
    for (int i = len - lvl; i < len; i++) y[p++] = i;
    for (int i = 0; i < len; i++)
      if (sa[i] >= lvl) y[p++] = sa[i] - lvl;
    memset(buk, 0, sizeof buk);
    for (int i = 0; i < len; i++) ++buk[x[y[i]]];
    for (int i = 1; i <= chr; i++) buk[i] += buk[i - 1];
    for (int i = len - 1; i >= 0; i--) sa[--buk[x[y[i]]]] = y[i];
    swap(x, y), x[sa[0]] = 0, p = 0;
    for (int i = 1; i < len; i++)
      if ((y[sa[i]] == y[sa[i - 1]]) && (y[sa[i] + lvl] == y[sa[i - 1] + lvl]))
        x[sa[i]] = p;
      else
        x[sa[i]] = ++p;
  }
}
inline void suffixHeight(int *A, int len, int *sa, int *he, int *rk) {
  int chr = 0;
  A[len] = 0;
  for (int i = 0; i < len; i++) chr = max(chr, A[i]);
  suffixArray(A, len + 1, chr, sa);
  for (int i = 1; i <= len; i++) rk[sa[i]] = i;
  int k = 0;
  for (int i = 0; i < len; i++) {
    for (k ? k-- : 0; A[i + k] == A[sa[rk[i] - 1] + k]; k++)
      ;
    he[rk[i]] = k;
  }
}
int fwd[MAXN * 2][18], bwd[MAXN * 2][18];
int sa[MAXN * 2], he[MAXN * 2], rk[MAXN * 2];
inline void build(int *A, int len) {
  suffixHeight(A, len, sa, he, rk);
  for (int i = 1; i <= len; i++) fwd[i][0] = he[i], bwd[i][0] = he[i + 1];
  for (int lvl = 1; lvl < 18; lvl++)
    for (int i = 1; i <= len; i++) {
      fwd[i][lvl] = fwd[i][lvl - 1];
      if (i - (1 << (lvl - 1)) > 0)
        fwd[i][lvl] = min(fwd[i][lvl], fwd[i - (1 << (lvl - 1))][lvl - 1]);
      bwd[i][lvl] = bwd[i][lvl - 1];
      if (i + (1 << (lvl - 1)) <= len)
        bwd[i][lvl] = min(bwd[i][lvl], bwd[i + (1 << (lvl - 1))][lvl - 1]);
    }
}
int str_len;
int rec[MAXN * 2];
int linr[MAXN * 2];
inline void solve(int *str, int len) {
  str_len = len;
  str[len] = INF;
  CrmTree::len = str_len - 1;
  for (int i = 0; i < len; i++) str[len + i + 1] = -str[i];
  memcpy(linr, str, 4 * (len * 2 + 1));
  sort(linr, linr + len * 2 + 1);
  int linr_len = (unique(linr, linr + len * 2 + 1) - linr) - 1;
  for (int i = 0; i <= len * 2; i++)
    str[i] = lower_bound(linr, linr + linr_len + 1, str[i]) - linr + 1;
  build(str, len * 2 + 1);
  for (int i = len + 1; i <= len * 2; i++) rec[rk[i]] = i;
  for (int i = 1; i <= len * 2 + 1; i++)
    if (rec[i])
      CrmTree::handleInsert(rec[i] - len - 1);
    else
      CrmTree::passOn();
}
inline pair<int, int> query(int l, int r) {
  int lim = r - l + 1;
  int pos = rk[l];
  int lef = pos, rig = pos;
  for (int i = 17; i >= 0; i--)
    if (fwd[lef][i] >= lim) lef -= (1 << i);
  for (int i = 17; i >= 0; i--)
    if (bwd[rig][i] >= lim) rig += (1 << i);
  return make_pair(lef, rig);
}
inline int handleQuery(int l, int r) {
  pair<int, int> bnd = query(l, r);
  int cur_len = r - l + 1;
  return CrmTree::queryRect(bnd.first, bnd.second, 0, l - cur_len - 1) +
         CrmTree::queryRect(bnd.first, bnd.second, r + 2, str_len - 1);
}
}  // namespace SuffixArray
inline int getInt() {
  int ret = 0;
  char ch;
  bool flg = false;
  while ((ch = getchar()) < '0' || ch > '9') flg |= (ch == '-');
  ret = ch - '0';
  while ((ch = getchar()) >= '0' && ch <= '9') ret = ret * 10 + (ch - '0');
  return flg ? -ret : ret;
}
int A[MAXN], delta[MAXN], linr[MAXN];
int str[MAXN * 2];
int main() {
  int N = getInt();
  for (int i = 1; i <= N; i++) A[i] = getInt();
  for (int i = 2; i <= N; i++) delta[i] = A[i] - A[i - 1];
  for (int i = 2; i <= N; i++) str[i - 2] = delta[i];
  SuffixArray::solve(str, N - 1);
  int Q = getInt();
  while (Q--) {
    int l = getInt(), r = getInt();
    if (l == r)
      printf("%d\n", N - 1);
    else
      printf("%d\n", SuffixArray::handleQuery(l - 1, r - 2));
  }
}