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;
bool debug = false;
int n, m, k;
int dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0};
long long ln, lk, lm;
const int MAXS = 100050, MAXT = 530000;
bool dbg = false;
struct SuffixArray {
  pair<int, int> suffix[100050 * 2];
  int sa[100050 * 2], rank[100050 * 2], height[100050 * 2], h[100050 * 2];
  void init(int n, const int *a) {
    sa[0] = 0;
    for (int i = 0; i < n; ++i) {
      rank[i] = a[i];
    }
    for (int m = 1; m < n; m <<= 1) {
      for (int i = 0; i < n; ++i) {
        sa[i] = i;
        suffix[i] = {rank[i], i + m < n ? rank[i + m] : -1};
      }
      sort(sa, sa + n,
           [&](int lhs, int rhs) { return suffix[lhs] < suffix[rhs]; });
      for (int i = 0; i < n; ++i) {
        if (i == 0 || (suffix[sa[i]] != suffix[sa[i - 1]])) {
          rank[sa[i]] = i;
        } else {
          rank[sa[i]] = rank[sa[i - 1]];
        }
      }
    }
    for (int i = 0; i < n; ++i) {
      if (rank[i] == 0) {
        h[i] = 0;
      } else {
        int x = i, y = sa[rank[i] - 1], z = max(0, h[i - 1] - 1);
        while (x + z < n && y + z < n && a[x + z] == a[y + z]) {
          ++z;
        }
        h[i] = z;
      }
    }
    for (int i = 0; i < n; ++i) {
      height[i] = h[sa[i]];
    }
  }
} sa1;
const int mn = 100050 * 2, ml = 20;
struct tree {
  int tt, rt[mn], le[mn * ml], ri[mn * ml], s[mn * ml];
  void add(const int t, int &p, int l, int r, int k) {
    s[p = ++tt] = s[t] + 1;
    if (l == r) return;
    int m = (l + r) / 2;
    if (k <= m)
      add(le[t], le[p], l, m, k), ri[p] = ri[t];
    else
      add(ri[t], ri[p], m + 1, r, k), le[p] = le[t];
  }
  int calc(int p, int l, int r, int x, int y) {
    if (x <= l && y >= r) return s[p];
    int m = (l + r) / 2, res = 0;
    if (x <= m) res += calc(le[p], l, m, x, y);
    if (y > m) res += calc(ri[p], m + 1, r, x, y);
    return res;
  }
  int query(int a, int b, int x, int y) {
    if (x > y) return 0;
    int res = calc(rt[b], 1, n, x, y) - calc(rt[a - 1], 1, n, x, y);
    return res;
  }
} Tre;
int a[100050 * 2], d[100050 * 2], M, T[MAXT], ss[100050 * 2];
map<int, int> id;
int f[mn][ml], log1[mn], bi[ml], L, h[mn];
int sa[mn], rk[mn];
bool cmp(int i, int j) { return a[i] < a[j]; }
void suffix() {
  int i, j, m, p = 1, t;
  for (i = 1; i <= L; ++i) sa[i] = i;
  sort(sa + 1, sa + 1 + L, cmp);
  rk[sa[1]] = 1;
  for (i = 2; i <= L; ++i)
    rk[sa[i]] = rk[sa[i - 1]] + (a[sa[i]] != a[sa[i - 1]]);
  for (m = rk[sa[L]]; m < L; p += p) {
    t = 0;
    for (i = 1; i <= L; ++i)
      if (sa[i] + p > L) h[++t] = sa[i];
    for (i = 1; i <= L; ++i)
      if (sa[i] > p) h[++t] = sa[i] - p;
    memset(ss, 0, sizeof(int) * (m + 2));
    for (i = 1; i <= L; ++i) ++ss[rk[i]];
    for (i = 1; i <= m; ++i) ss[i] += ss[i - 1];
    for (int i = L; i >= 1; i--) sa[ss[rk[h[i]]]--] = h[i];
    memcpy(h, rk, sizeof(int) * (L + 2)), rk[sa[1]] = 1;
    for (i = 2; i <= L; ++i)
      rk[sa[i]] = rk[sa[i - 1]] + (h[sa[i]] != h[sa[i - 1]] ||
                                   h[sa[i] + p] != h[sa[i - 1] + p]);
    m = rk[sa[L]];
  }
  h[1] = t = 0;
  for (i = 1; i <= L; ++i) {
    if (rk[i] == 1) continue;
    if (t > 0) --t;
    for (j = sa[rk[i] - 1]; a[i + t] == a[j + t]; ++t)
      ;
    h[rk[i]] = t;
  }
}
void solve() {
  int i, j;
  ss[0] = 0, log1[1] = 0;
  for (i = 1; i <= L; ++i) f[i][0] = h[i];
  for (i = 2; i <= L; ++i) log1[i] = log1[i >> 1] + 1;
  for (i = 1; i <= L; ++i) {
    ss[i] = ss[i - 1];
    if (sa[i] > n + 1) {
      ++ss[i];
      Tre.add(Tre.rt[i - 1], Tre.rt[i], 1, n, sa[i] - n - 1);
    } else
      Tre.rt[i] = Tre.rt[i - 1];
  }
  for (i = 0; i <= log1[L]; ++i) bi[i] = 1 << i;
  for (j = 1; j <= log1[L]; ++j)
    for (i = 1; i <= L - bi[j] + 1; ++i)
      f[i][j] = min(f[i][j - 1], f[i + bi[j - 1]][j - 1]);
}
int queryo(int l, int r) {
  if (l > r) return 1 << 30;
  int k = log1[r - l + 1];
  return min(f[l][k], f[r - bi[k] + 1][k]);
}
int calc(int l, int r) {
  int hi = r - l + 1, a, b, x, y, m, res;
  x = 1, y = rk[l];
  while (x < y) {
    m = (x + y) / 2;
    if (queryo(m + 1, rk[l]) < hi)
      x = m + 1;
    else
      y = m;
  }
  a = x;
  x = rk[l], y = L;
  while (x < y) {
    m = (x + y + 1) / 2;
    if (queryo(rk[l] + 1, m) < hi)
      y = m - 1;
    else
      x = m;
  }
  b = x;
  res = ss[b] - ss[a - 1] - Tre.query(a, b, max(1, l - hi), min(r + 1, n));
  return res;
}
int main() {
  int la, i, j;
  scanf("%d%d", &n, &la);
  --n;
  for (i = 1; i <= n; ++i) {
    scanf("%d", &j);
    a[i] = j - la, la = j;
  }
  a[L = n + 1] = 1 << 30;
  for (i = 1; i <= n; ++i) a[++L] = -a[i];
  a[L + 1] = (1 << 30) + 1;
  int cnt = 1;
  for (i = 1; i <= L + 1; ++i) id[a[i]] = cnt++;
  for (i = 1; i <= L + 1; ++i) d[i - 1] = id[a[i]];
  suffix();
  int q, a, b;
  scanf("%d", &q);
  solve();
  while (q--) {
    scanf("%d%d", &a, &b);
    if (a == b)
      printf("%d\n", n);
    else
      printf("%d\n", calc(a, b - 1));
  }
  return 0;
}