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;
int n, len;
int s[400005];
int sa[400005], rk[400005], h[400005], wa[400005], wb[400005], c[400005];
int F[23][400005], lg[400005];
int ans[400005];
template <class T>
inline bool cmp(T *r, int a, int b, int l) {
  if (a + l >= len || b + l >= len) return 0;
  return r[a] == r[b] && r[a + l] == r[b + l];
}
inline bool sort_cmp(const int &a, const int &b) { return s[a] < s[b]; }
inline void build(int n) {
  int i, j, p, m, *x = wa, *y = wb, *t;
  for (i = 0; i < n; i++) sa[i] = i;
  sort(sa, sa + n, sort_cmp);
  for (i = 1, p = 1, x[sa[0]] = 0; i < n; i++)
    x[sa[i]] = cmp(s, sa[i], sa[i - 1], 0) ? p - 1 : p++;
  m = p;
  for (j = 1, p = 0; j < n && p < n; j <<= 1, m = p) {
    for (p = 0, i = n - j; i < n; i++) y[p++] = i;
    for (i = 0; i < n; i++)
      if (sa[i] >= j) y[p++] = sa[i] - j;
    for (i = 0; i < m; i++) c[i] = 0;
    for (i = 0; i < n; i++) c[x[y[i]]]++;
    for (i = 1; i < m; i++) c[i] += c[i - 1];
    for (i = n - 1; i >= 0; i--) sa[--c[x[y[i]]]] = y[i];
    for (t = x, x = y, y = t, i = 1, p = 1, x[sa[0]] = 0; i < n; i++)
      x[sa[i]] = cmp(y, sa[i], sa[i - 1], j) ? p - 1 : p++;
  }
}
inline void Cal(int n) {
  int i, j, k = 0;
  for (i = 0; i < n; i++) rk[sa[i]] = i;
  for (i = 0; i < n; h[rk[i++]] = k)
    for (k ? k-- : 0; rk[i] - 1 >= 0 && s[sa[rk[i] - 1] + k] == s[i + k]; k++)
      ;
  for (j = 1; j < n; j++) F[0][j] = h[j];
  for (j = 1; j < 23; j++)
    for (i = 1; i < n; i++)
      F[j][i] = min(F[j - 1][i], F[j - 1][i + (1 << j - 1)]);
}
inline int ser(int u, int v) {
  int tmp = lg[v - u + 1];
  return min(F[tmp][u], F[tmp][v - (1 << tmp) + 1]);
}
inline int Search1(int u, int Len) {
  int L = 0, R = u, Mid;
  while (L < R) {
    Mid = (L + R) >> 1;
    if (ser(Mid + 1, u) >= Len)
      R = Mid;
    else
      L = Mid + 1;
  }
  return L;
}
inline int Search2(int u, int Len) {
  int L = u, R = len - 1, Mid;
  while (L < R) {
    Mid = (L + R + 1) >> 1;
    if (ser(u + 1, Mid) >= Len)
      L = Mid;
    else
      R = Mid - 1;
  }
  return L;
}
int cnt_q;
struct Query {
  int dlim, ulim, x, id, typ;
  Query() {}
  Query(int dlim, int ulim, int x, int id, int typ)
      : dlim(dlim), ulim(ulim), x(x), id(id), typ(typ) {}
  bool operator<(const Query &nxt) const { return x < nxt.x; }
} Q[400005 * 2];
struct BIT {
  int arr[400005 * 2];
  void upd(int now, int val) {
    if (now == 0) return;
    for (; now <= len + 1; now += now & -now) arr[now] += val;
  }
  int qsum(int now) {
    int ret = 0;
    for (; now > 0; now -= now & -now) ret += arr[now];
    return ret;
  }
} T;
int main() {
  for (int sum = 1, i = 0; sum < 400005; sum <<= 1, i++) lg[sum] = i;
  for (int pre = 0, i = 0; i < 400005; i++) lg[i] = (pre = max(pre, lg[i]));
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    scanf("%d", &s[i]);
    if (i) s[i - 1] = s[i] - s[i - 1];
  }
  s[n - 1] = -1 << 30;
  for (int i = n; i <= 2 * n - 2; i++) s[i] = -s[i - n];
  len = 2 * n - 1;
  build(len);
  Cal(len);
  int q;
  scanf("%d", &q);
  for (int u, v, i = 1; i <= q; i++) {
    scanf("%d%d", &u, &v);
    if (u == v)
      ans[i] = n - 1;
    else {
      u--, v -= 2;
      int pos = rk[u + n];
      int pos1 = Search1(pos, v - u + 1), pos2 = Search2(pos, v - u + 1);
      Q[cnt_q++] = Query(pos1 - 1, pos2, u - (v - u + 1) - 1, i, -1);
      Q[cnt_q++] = Query(pos1 - 1, pos2, v + 1, i, 1);
    }
  }
  sort(Q, Q + cnt_q);
  int X = 0;
  for (int i = 0; i < cnt_q; i++) {
    for (; X <= Q[i].x; X++) T.upd(rk[X] + 1, 1);
    ans[Q[i].id] -= Q[i].typ * (T.qsum(Q[i].ulim + 1) - T.qsum(Q[i].dlim + 1));
  }
  for (; X < n - 1; X++) T.upd(rk[X] + 1, 1);
  for (int i = 0; i < cnt_q; i++)
    if (Q[i].typ == 1)
      ans[Q[i].id] += T.qsum(Q[i].ulim + 1) - T.qsum(Q[i].dlim + 1);
  for (int i = 1; i <= q; i++) printf("%d\n", ans[i]);
}