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>
const int INF = 0x3f3f3f3f;
const long long int INFLL = 0x3f3f3f3f3f3f3f3fll;
long long int getint() {
  long long int ret = 0;
  bool f = 0;
  char c;
  while ((c = getchar()) < '0' || c > '9')
    if (c == '-') f ^= 1;
  while (c >= '0' && c <= '9') {
    ret = ret * 10 + c - '0';
    c = getchar();
  }
  return f ? -ret : ret;
}
using namespace std;
const int MAXN = 200000;
namespace SA {
int sa[MAXN + 10], *rk, h[MAXN + 10];
int wa[MAXN + 10], wb[MAXN + 10], ws[MAXN + 10], wv[MAXN + 10];
bool cmp(int *rk, int i, int j, int len) {
  return rk[i] == rk[j] && rk[i + len] == rk[j + len];
}
void calsa(int *r, int n, int m) {
  int *x = wa, *y = wb, i, j, p;
  for (i = 1; i <= m; ++i) ws[i] = 0;
  for (i = 1; i <= n; ++i) ++ws[x[i] = r[i]];
  for (i = 1; i <= m; ++i) ws[i] += ws[i - 1];
  for (i = n; i >= 1; --i) sa[ws[x[i]]--] = i;
  for (j = 1, p = 1; p < n; m = p, j <<= 1) {
    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 = 1; i <= m; ++i) ws[i] = 0;
    for (i = 1; i <= n; ++i) ++ws[wv[i] = x[y[i]]];
    for (i = 1; i <= m; ++i) ws[i] += ws[i - 1];
    for (i = n; i >= 1; --i) sa[ws[wv[i]]--] = y[i];
    for (swap(x, y), x[sa[1]] = 1, i = 2, p = 1; i <= n; ++i)
      x[sa[i]] = cmp(y, sa[i], sa[i - 1], j) ? p : ++p;
  }
  rk = x;
}
void calh(int *r, int n) {
  for (int i = 1, k = 0, j; i <= n; h[rk[i++]] = k)
    for (k ? --k : 0, j = sa[rk[i] + 1]; r[i + k] == r[j + k]; ++k)
      ;
}
}  // namespace SA
using SA::h;
using SA::rk;
using SA::sa;
int a[MAXN + 10], hsh[MAXN + 10], n, N;
int st[MAXN + 10][20], lg[MAXN + 10];
int L[MAXN + 10], R[MAXN + 10], ans[MAXN + 10];
void init() {
  lg[0] = -1;
  for (int i = 1; i <= N; ++i) {
    st[i][0] = h[i];
    lg[i] = lg[i >> 1] + 1;
  }
  for (int j = 1; j <= lg[N]; ++j) {
    int len = 1 << (j - 1);
    for (int i = 1; i + (len << 1) - 1 <= N; ++i)
      st[i][j] = min(st[i][j - 1], st[i + len][j - 1]);
  }
}
int query(int l, int r) {
  if (l == r) return INF;
  int t = lg[r - l];
  return min(st[l][t], st[r - (1 << t)][t]);
}
void getseq(int p, int len, int &pl, int &pr) {
  int l = 1, r = p;
  while (l <= r) {
    int mid = (l + r) >> 1;
    if (query(mid, p) >= len) {
      pl = mid;
      r = mid - 1;
    } else
      l = mid + 1;
  }
  l = p;
  r = N;
  while (l <= r) {
    int mid = (l + r) >> 1;
    if (query(p, mid) >= len) {
      pr = mid;
      l = mid + 1;
    } else
      r = mid - 1;
  }
}
int c[MAXN + 10];
vector<int> vec[MAXN + 10];
void update(int p) {
  while (p <= N) ++c[p], p += p & -p;
}
int query(int p) {
  int ret = 0;
  while (p) ret += c[p], p -= p & -p;
  return ret;
}
int querytr(int l, int r) {
  if (l > r) return 0;
  return query(r) - query(l - 1);
}
int main() {
  n = getint();
  for (int i = 1; i <= n; ++i) a[i] = getint();
  n--;
  N = 2 * n;
  for (int i = 1; i <= n; ++i) hsh[i] = a[i] = a[i + 1] - a[i];
  for (int i = n + 1; i <= N; ++i) hsh[i] = a[i] = -a[i - n];
  sort(hsh + 1, hsh + N + 1);
  int m = unique(hsh + 1, hsh + N + 1) - hsh - 1;
  for (int i = 1; i <= N; ++i)
    a[i] = lower_bound(hsh + 1, hsh + m + 1, a[i]) - hsh;
  SA::calsa(a, N, m);
  SA::calh(a, N);
  init();
  int q = getint();
  for (int i = 1; i <= q; ++i) {
    int &l = L[i] = getint(), &r = R[i] = getint();
    if (l == r) {
      ans[i] = n;
      continue;
    }
    --r;
    int p = rk[l + n], pl, pr;
    getseq(p, r - l + 1, pl, pr);
    vec[pl - 1].push_back(-i);
    vec[pr].push_back(i);
  }
  for (int i = 1; i <= N; ++i) {
    update(sa[i]);
    for (int j = 0; j < vec[i].size(); ++j) {
      int id = vec[i][j], flag = 1;
      if (id < 0) id = -id, flag = -1;
      ans[id] += flag * (querytr(1, L[id] - (R[id] - L[id] + 2)) +
                         querytr(R[id] + 2, n - R[id] + L[id]));
    }
  }
  for (int i = 1; i <= q; ++i) printf("%d\n", ans[i]);
  return 0;
}