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;
struct pod {
  int l, p, nr;
};
const int INF = 5000000000;
bool por(pod a, pod b) {
  if (a.l < b.l) return true;
  if (a.l > b.l) return false;
  if (a.p < b.p) return true;
  if (a.p > b.p) return false;
  return a.nr < b.nr;
}
vector<int> T1;
int l1, size1;
void Stworz1(int n) {
  int p = 1;
  while (p < n) p *= 2;
  l1 = p;
  size1 = 2 * p - 1;
  for (int i = 0; i <= size1; ++i) T1.push_back(INF);
}
void Dodaj1(int a, int x) {
  a += l1;
  T1[a] = x;
  a /= 2;
  while (a != 0) {
    T1[a] = min(T1[2 * a], T1[2 * a + 1]);
    a /= 2;
  }
}
int Mini1(int a, int b) {
  a += l1;
  b += l1;
  int w = min(T1[a], T1[b]);
  while (a / 2 != b / 2) {
    if (a % 2 == 0) w = min(w, T1[a + 1]);
    if (b % 2 == 1) w = min(w, T1[b - 1]);
    a /= 2;
    b /= 2;
  }
  return w;
}
vector<vector<int> > T2;
int l2, size2;
void Stworz2(int n) {
  int p = 1;
  while (p < n) p *= 2;
  l2 = p;
  size2 = 2 * p - 1;
  vector<int> t;
  for (int i = 0; i <= size2; ++i) T2.push_back(t);
}
void Dodaj2(int a, int x) {
  a += l2;
  while (a != 0) {
    T2[a].push_back(x);
    a /= 2;
  }
}
int Szukaj1(vector<int>& a, int x) {
  int l = 0, p = a.size() - 1, s, r = -1;
  while (l <= p) {
    s = (l + p) / 2;
    if (a[s] >= x) {
      r = s;
      p = s - 1;
    } else
      l = s + 1;
  }
  return r;
}
int Szukaj2(vector<int>& a, int x) {
  int l = 0, p = a.size() - 1, s, r = -1;
  while (l <= p) {
    s = (l + p) / 2;
    if (a[s] <= x) {
      r = s;
      l = s + 1;
    } else
      p = s - 1;
  }
  return r;
}
int f(vector<int>& a, int l, int r) {
  int L = Szukaj1(a, l);
  int P = Szukaj2(a, r);
  if (L != -1 && P != -1 && L <= P) return P - L + 1;
  return 0;
}
int Ile2(int a, int b, int l, int r) {
  if (l > r) return 0;
  a += l2;
  b += l2;
  int w = f(T2[a], l, r);
  if (a != b) w += f(T2[b], l, r);
  while (a / 2 != b / 2) {
    if (a % 2 == 0) w += f(T2[a + 1], l, r);
    if (b % 2 == 1) w += f(T2[b - 1], l, r);
    a /= 2;
    b /= 2;
  }
  return w;
}
void SuffixArray(vector<int> a, vector<int>& sa, vector<int>& lcp, int p) {
  vector<vector<int> > kmr(p + 1);
  for (int i = 0; i < a.size(); ++i) kmr[0].push_back(a[i]);
  for (int i = 1; i <= p; ++i) {
    vector<pod> t;
    for (int j = 0; j + (1 << i) - 1 < a.size(); ++j) {
      t.push_back({kmr[i - 1][j], kmr[i - 1][j + (1 << (i - 1))], j});
      kmr[i].push_back(-1);
    }
    sort(t.begin(), t.end(), por);
    int nr = 0;
    for (int j = 0; j < t.size(); ++j) {
      if (j != 0 && (t[j].l != t[j - 1].l || t[j].p != t[j - 1].p)) ++nr;
      kmr[i][t[j].nr] = nr;
    }
  }
  sa = kmr[p];
  vector<int> gdzie(sa.size());
  for (int i = 0; i < sa.size(); ++i) gdzie[sa[i]] = i;
  for (int i = 0; i < sa.size(); ++i) {
    if (sa[i] == sa.size() - 1) continue;
    int d = 0;
    if (i != 0 && sa[i - 1] != sa.size() - 1)
      d = max(0, (int)lcp[sa[i - 1]] - 1);
    while (a[i + d] == a[gdzie[sa[i] + 1] + d]) ++d;
    lcp[sa[i]] = d;
  }
}
int Szukaj3(vector<int>& a, int L, int P, int x) {
  int l = L, p = P, s, r = -1;
  while (l <= p) {
    s = (l + p) / 2;
    if (Mini1(s, P) >= x) {
      r = s;
      p = s - 1;
    } else
      l = s + 1;
  }
  return r;
}
int Szukaj4(vector<int>& a, int L, int P, int x) {
  int l = L, p = P, s, r = -1;
  while (l <= p) {
    s = (l + p) / 2;
    if (Mini1(L, s) >= x) {
      r = s;
      l = s + 1;
    } else
      p = s - 1;
  }
  return r;
}
int main() {
  ios_base::sync_with_stdio(0);
  int n;
  cin >> n;
  vector<int> h(n);
  for (int i = 0; i < n; ++i) cin >> h[i];
  if (n == 1) {
    int q;
    cin >> q;
    for (int i = 0; i < q; ++i) cout << "0" << endl;
    return 0;
  }
  vector<int> a;
  for (int i = 0; i < n - 1; ++i) a.push_back(h[i] - h[i + 1]);
  a.push_back(INF);
  for (int i = 0; i < n - 1; ++i) a.push_back(h[i + 1] - h[i]);
  int m = a.size();
  int p = 0;
  while ((1 << p) < m) ++p;
  for (int i = 0; i < (1 << p) - 1; ++i) a.push_back(INF);
  vector<int> sa(m), lcp(m - 1);
  SuffixArray(a, sa, lcp, p);
  Stworz1(m - 1);
  for (int i = 0; i < m - 1; ++i) Dodaj1(i, lcp[i]);
  Stworz2(m);
  for (int i = 0; i < m; ++i) {
    if (i <= n - 1)
      Dodaj2(sa[i], -1);
    else
      Dodaj2(sa[i], i - n);
  }
  for (int i = 1; i <= size2; ++i) sort(T2[i].begin(), T2[i].end());
  int q;
  cin >> q;
  vector<int> gdzie(sa.size());
  for (int i = 0; i < m; ++i) gdzie[sa[i]] = i;
  for (int i = 0; i < q; ++i) {
    int l, r;
    cin >> l >> r;
    --l;
    --r;
    if (l == r) {
      cout << n - 1 << endl;
      continue;
    }
    int nr = sa[l], d = r - l + 1;
    int L = Szukaj3(lcp, 0, nr - 1, d - 1);
    int P = Szukaj4(lcp, nr, m - 1, d - 1);
    int odp;
    if (L == -1 && P == -1) odp = 0;
    if (L == -1 && P != -1) {
      odp = Ile2(nr, P, 0, l - d) + Ile2(nr, P, r + 1, n - 2);
      if (gdzie[P + 1] >= n &&
          !(gdzie[P + 1] >= l - d + 1 + n && gdzie[P + 1] <= r + n))
        ++odp;
    }
    if (L != -1 && P == -1) {
      odp = Ile2(L, nr - 1, 0, l - d) + Ile2(L, nr - 1, r + 1, n - 2);
      if (gdzie[nr] >= n && !(gdzie[nr] >= l - d + 1 + n && gdzie[nr] <= r + n))
        ++odp;
    }
    if (L != -1 && P != -1) {
      odp = Ile2(L, P, 0, l - d) + Ile2(L, P, r + 1, n - 2);
      if (gdzie[P + 1] >= n &&
          !(gdzie[P + 1] >= l - d + 1 + n && gdzie[P + 1] <= r + n))
        ++odp;
    }
    cout << odp << endl;
  }
  return 0;
}