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 INF = 1000000005;
const int maxn = 200005;
int data[maxn];
int SA[maxn], rnk[maxn], H[maxn], N;
bool cmp(int a, int b) { return data[a] < data[b]; }
int c[maxn], a[maxn], _SA[maxn], _rnk[maxn], _h[maxn], L;
void Double() {
  memset(c, 0, sizeof(int) * (N + 1));
  int i;
  for (i = 1; i <= N; i++) c[((i + L) > N ? 0 : rnk[i + L])]++;
  a[0] = 1;
  for (i = 1; i <= N; i++) a[i] = a[i - 1] + c[i - 1];
  for (i = 1; i <= N; i++) _SA[a[((i + L) > N ? 0 : rnk[i + L])]++] = i;
  memset(c, 0, sizeof(int) * (N + 1));
  for (i = 1; i <= N; i++) c[((i) > N ? 0 : rnk[i])]++;
  a[0] = 1;
  for (i = 1; i <= N; i++) a[i] = a[i - 1] + c[i - 1];
  for (i = 1; i <= N; i++) SA[a[((_SA[i]) > N ? 0 : rnk[_SA[i]])]++] = _SA[i];
  _rnk[SA[1]] = 1;
  for (int i = 2; i <= N; i++)
    if (((SA[i]) > N ? 0 : rnk[SA[i]]) ==
            ((SA[i - 1]) > N ? 0 : rnk[SA[i - 1]]) &&
        ((SA[i] + L) > N ? 0 : rnk[SA[i] + L]) ==
            ((SA[i - 1] + L) > N ? 0 : rnk[SA[i - 1] + L]))
      _rnk[SA[i]] = _rnk[SA[i - 1]];
    else
      _rnk[SA[i]] = _rnk[SA[i - 1]] + 1;
  memcpy(rnk, _rnk, sizeof(int) * (N + 1));
  L <<= 1;
}
void make_SA() {
  int i;
  for (i = 1; i <= N; i++) SA[i] = i;
  sort(SA + 1, SA + N + 1, cmp);
  rnk[SA[1]] = 1;
  for (int i = 2; i <= N; i++)
    if (data[SA[i]] == data[SA[i - 1]])
      rnk[SA[i]] = rnk[SA[i - 1]];
    else
      rnk[SA[i]] = rnk[SA[i - 1]] + 1;
  L = 1;
  while (L < N) Double();
}
void make_H() {
  int i;
  memset(_h, 0, sizeof(int) * (N + 1));
  for (i = 1; i <= N; i++)
    if (rnk[i] == 1)
      _h[i] = 0;
    else if (i == 1 || _h[i - 1] <= 1)
      while (data[i + _h[i]] == data[SA[rnk[i] - 1] + _h[i]]) _h[i]++;
    else {
      _h[i] = _h[i - 1] - 1;
      while (data[i + _h[i]] == data[SA[rnk[i] - 1] + _h[i]]) _h[i]++;
    }
  for (i = 1; i <= N; i++) H[rnk[i]] = _h[i];
}
int n, A[100005];
int Lcp[200005][20];
void get_Lcp() {
  for (int i = 1; i <= N; i++) {
    Lcp[i][1] = H[i + 1];
  }
  for (int l = 2; (1 << l) <= N; l++) {
    for (int i = 1; i <= N + 1 - (1 << l); i++) {
      Lcp[i][l] = min(Lcp[i][l - 1], Lcp[i + (1 << (l - 1))][l - 1]);
      Lcp[i][l] = min(Lcp[i][l], H[i + (1 << (l - 1))]);
    }
  }
}
int C[maxn * 30], ls[maxn * 30], rs[maxn * 30], scc;
int head[maxn];
void build(int l, int r, int o) {
  C[o] = 0;
  if (l == r) return;
  ls[o] = (++scc);
  rs[o] = (++scc);
  int mid = (l + r) / 2;
  build(l, mid, ls[o]);
  build(mid + 1, r, rs[o]);
}
void add(int l, int r, int preo, int& o, int x) {
  o = (++scc);
  C[o] = C[preo] + 1;
  if (l == r) {
    return;
  }
  int mid = (l + r) / 2;
  if (x <= mid) {
    rs[o] = rs[preo];
    add(l, mid, ls[preo], ls[o], x);
  } else {
    ls[o] = ls[preo];
    add(mid + 1, r, rs[preo], rs[o], x);
  }
}
int query(int l, int r, int o, int L, int R) {
  if (L <= l && r <= R) {
    return C[o];
  }
  int mid = (l + r) / 2;
  int res = 0;
  if (L <= mid) res += query(l, mid, ls[o], L, R);
  if (mid < R) res += query(mid + 1, r, rs[o], L, R);
  return res;
}
int cal(int l, int r, int a, int b) {
  if (a > b) return 0;
  return query(1, N, head[r], a, b) - query(1, N, head[l - 1], a, b);
}
int pw[maxn];
int found_up(int s, int t, int l) {
  int _s = s, _t = t - 1, ret = t;
  while (_s <= _t) {
    int mid = (_s + _t) / 2;
    int dis = pw[t - mid + 1];
    int h = min(Lcp[mid][dis], Lcp[t + 1 - (1 << dis)][dis]);
    if (h < l) {
      _s = mid + 1;
    } else {
      _t = mid - 1;
      ret = mid;
    }
  }
  return ret;
}
int found_down(int s, int t, int l) {
  int _s = s + 1, _t = t, ret = s;
  while (_s <= _t) {
    int mid = (_s + _t) / 2;
    int dis = pw[mid - s + 1];
    int h = min(Lcp[s][dis], Lcp[mid + 1 - (1 << dis)][dis]);
    if (h < l) {
      _t = mid - 1;
    } else {
      _s = mid + 1;
      ret = mid;
    }
  }
  return ret;
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) scanf("%d", A + i);
  for (int i = n; i >= 2; i--) A[i] -= A[i - 1];
  N = 2 * n - 1;
  for (int i = 1; i < n; i++) data[i] = A[i + 1];
  data[n] = -INF;
  for (int i = n + 1; i < (n << 1); i++) data[i] = -A[i - n + 1];
  make_SA();
  make_H();
  get_Lcp();
  scc = 0;
  head[0] = 0;
  build(1, N, 0);
  for (int i = 1; i <= N; i++) {
    add(1, N, head[i - 1], head[i], SA[i]);
  }
  for (int i = 1; i <= N; i++) {
    for (int j = 0;; j++) {
      if ((1 << j) <= i) continue;
      pw[i] = j - 1;
      break;
    }
  }
  int q, s, t;
  scanf("%d", &q);
  while (q--) {
    scanf("%d%d", &s, &t);
    if (s == t) {
      printf("%d\n", n - 1);
      continue;
    }
    s += n;
    t += (n - 1);
    int l = found_up(1, rnk[s], t - s + 1);
    int r = found_down(rnk[s], N, t - s + 1);
    int ans = cal(l, r, t - n + 2, n - 1) + cal(l, r, 1, 2 * s - n - t - 2);
    printf("%d\n", ans);
  }
  return 0;
}