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;
inline int Read() {
  char c = getchar();
  int num = 0;
  while ('0' > c || c > '9') c = getchar();
  while ('0' <= c && c <= '9') num = num * 10 + c - '0', c = getchar();
  return (num);
}
int wa[300010], wb[300010], wd[300010], h[300010], sa[300010], rk[300010],
    r[300010], len, n, m;
inline bool Cmp(int *r, int a, int b, int l) {
  return (r[a] == r[b] && r[a + l] == r[b + l]);
}
inline void Get_sa(int n, int m) {
  int *x = wa, *y = wb, *t, i, j, k, p;
  for (i = 1; i <= n; i++) wd[x[i] = r[i]]++;
  for (i = 1; i <= m; i++) wd[i] += wd[i - 1];
  for (i = n; i >= 1; i--) sa[wd[x[i]]--] = i;
  for (j = 1; j <= n; j *= 2, m = p) {
    for (p = 0, i = n - j + 1; i <= n; i++) y[++p] = i;
    for (i = 1; i <= n; i++)
      if (sa[i] > j) y[++p] = sa[i] - j;
    for (i = 0; i <= m; i++) wd[i] = 0;
    for (i = 1; i <= n; i++) wd[x[y[i]]]++;
    for (i = 1; i <= m; i++) wd[i] += wd[i - 1];
    for (i = n; i >= 1; i--) sa[wd[x[y[i]]]--] = y[i];
    for (p = 0, t = x, x = y, y = t, x[sa[1]] = 0, i = 2; i <= n; i++)
      if (Cmp(y, sa[i - 1], sa[i], j))
        x[sa[i]] = p;
      else
        x[sa[i]] = ++p;
  }
  for (i = 1; i <= n; i++) rk[sa[i]] = i;
  for (i = 1, k = 0; i < n; h[rk[i]] = k, i++)
    for ((k ? k-- : 0), j = sa[rk[i] - 1]; r[i + k] == r[j + k]; k++)
      ;
}
int rq[300010][18], ha[300010], a[300010];
inline void Get_st() {
  for (int i = 1; i <= len; i++) rq[i][0] = h[i];
  for (int j = 1; j <= 17; j++)
    for (int i = (1 << j); i <= len; i++)
      rq[i][j] = min(rq[i][j - 1], rq[i - (1 << j - 1)][j - 1]);
}
inline void String() {
  len = 0;
  int ar = 0, ar_;
  for (int i = 1; i <= n; i++) ha[++ar] = a[i] - a[i - 1];
  for (int i = 1; i <= n; i++) ha[++ar] = a[i - 1] - a[i];
  ar_ = ar, ar = 1;
  sort(ha + 1, ha + 1 + ar_);
  for (int i = 2; i <= ar_; i++)
    if (ha[i] != ha[i - 1]) ha[++ar] = ha[i];
  for (int i = 1; i <= n; i++)
    r[++len] = lower_bound(ha + 1, ha + 1 + ar, a[i] - a[i - 1]) - ha;
  r[++len] = ar + 1;
  for (int i = 1; i <= n; i++)
    r[++len] = lower_bound(ha + 1, ha + 1 + ar, a[i - 1] - a[i]) - ha;
  Get_sa(len + 1, ar + 1);
  Get_st();
}
int N, ch[300010 * 20][2], t[300010 * 20], rt[300010];
inline void Add(int rt1, int rt, int l, int r, int a) {
  t[rt] = t[rt1] + 1;
  int md = (l + r) / 2;
  if (l == r) return;
  if (a <= md) {
    ch[rt][0] = ++N;
    ch[rt][1] = ch[rt1][1];
    Add(ch[rt1][0], ch[rt][0], l, md, a);
  } else {
    ch[rt][1] = ++N;
    ch[rt][0] = ch[rt1][0];
    Add(ch[rt1][1], ch[rt][1], md + 1, r, a);
  }
}
inline int Ask(int rt, int l, int r, int a, int b) {
  int md = (l + r) / 2;
  if (!rt || b < l || r < a) return (0);
  if (l == a && r == b) return (t[rt]);
  if (b <= md) return (Ask(ch[rt][0], l, md, a, b));
  if (md < a) return (Ask(ch[rt][1], md + 1, r, a, b));
  if (a <= md && md < b) {
    int L = Ask(ch[rt][0], l, md, a, md);
    int R = Ask(ch[rt][1], md + 1, r, md + 1, b);
    return (L + R);
  }
}
inline int Solv(int x, int y) {
  int lcp = y - x + 1, L = rk[x], R = rk[x];
  for (int i = 17; i >= 0; i--)
    if (L - (1 << i) > 0)
      if (rq[L][i] >= lcp) L = L - (1 << i);
  for (int i = 17; i >= 0; i--)
    if (R + (1 << i) <= len)
      if (rq[R + (1 << i)][i] >= lcp) R = R + (1 << i);
  int nm1 = 0, nm0 = 0;
  if (x - lcp - 1) {
    nm1 += Ask(rt[R], 1, len, 3 + n, x - lcp + n);
    nm0 += Ask(rt[L - 1], 1, len, 3 + n, x - lcp + n);
  }
  if (y + 1 < n) {
    nm1 += Ask(rt[R], 1, len, y + 3 + n, len);
    nm0 += Ask(rt[L - 1], 1, len, y + 3 + n, len);
  }
  return (max(nm1 - nm0, 0));
}
int main() {
  n = Read();
  for (int i = 1; i <= n; i++) a[i] = Read();
  String();
  for (int i = 2; i <= len + 1; i++) {
    rt[i] = ++N;
    Add(rt[i - 1], rt[i], 1, len, sa[i]);
  }
  m = Read();
  for (int cs = 1; cs <= m; cs++) {
    int x = Read(), y = Read();
    if (x == y)
      printf("%d\n", n - 1);
    else
      printf("%d\n", Solv(x + 1, y));
  }
}