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>
#pragma GCC optimize("O2")
using namespace std;
const int N = 2e5 + 10;
const long long mod = 1e9 + 7;
const long long mod2 = 998244353;
const long long inf = 8e18;
const int LOG = 21;
long long pw(long long a, long long b, long long M) {
  return (!b ? 1
             : (b & 1 ? (a * pw(a * a % M, b / 2, M)) % M
                      : pw(a * a % M, b / 2, M)));
}
int len[N], A[N], B[N], P[N], rnk[LOG][N], T, n, m;
bool cmp(int i, int j) {
  int t = T - 1;
  if (T && rnk[t][i] == rnk[t][j]) {
    if (max(i, j) + (1 << t) - 1 > m) return i > j;
    return rnk[t][i + (1 << t)] < rnk[t][j + (1 << t)];
  }
  return rnk[t][i] < rnk[t][j];
}
void Build() {
  for (int i = 1; i <= m; i++) {
    P[i] = i;
    rnk[0][i] = B[i];
  }
  for (T = 1; T < LOG; T++) {
    sort(P + 1, P + m + 1, cmp);
    rnk[T][P[1]] = 1;
    for (int i = 2; i <= m; i++) {
      rnk[T][P[i]] = rnk[T][P[i - 1]] + cmp(P[i - 1], P[i]);
    }
  }
}
int lcp(int i, int j) {
  if (min(i, j) <= 0 || max(i, j) > m) return 0;
  int res = 0, Mx = min(len[i], len[j]);
  for (T = LOG - 1; ~T; T--) {
    if (max(i, j) + (1 << T) - 1 > m || rnk[T][i] != rnk[T][j]) continue;
    res |= 1 << T;
    i += 1 << T;
    j += 1 << T;
  }
  return min({res, Mx});
}
vector<int> seg[4 * N], id[4 * N];
int Mn[4 * N], ps[N], LAST[N];
void Bu(int v, int tl, int tr) {
  if (tl == tr) {
    Mn[v] = lcp(P[tl - 1], P[tl]);
    if (P[tl] < n) {
      seg[v].push_back(P[tl]);
      id[v].push_back(0);
    }
    return;
  }
  int mid = (tl + tr) >> 1;
  Bu(v << 1, tl, mid);
  Bu(v << 1 | 1, mid + 1, tr);
  Mn[v] = min(Mn[v << 1], Mn[v << 1 | 1]);
  int t1 = (int)seg[v << 1].size(), t2 = (int)seg[v << 1 | 1].size(), i = 0,
      j = 0;
  id[v].push_back(0);
  while (i < t1 && j < t2) {
    if (seg[v << 1][i] < seg[v << 1 | 1][j]) {
      id[v].push_back(id[v].back() + 1);
      seg[v].push_back(seg[v << 1][i]);
      i++;
    } else {
      id[v].push_back(id[v].back());
      seg[v].push_back(seg[v << 1 | 1][j]);
      j++;
    }
  }
  for (; i < t1; i++) {
    id[v].push_back(id[v].back() + 1);
    seg[v].push_back(seg[v << 1][i]);
  }
  for (; j < t2; j++) {
    id[v].push_back(id[v].back());
    seg[v].push_back(seg[v << 1 | 1][j]);
  }
}
int Get_mn(int v, int tl, int tr, int l, int r) {
  if (r > m || l < 0) return 0;
  if (l > r || l > tr || r < tl) return 2e9;
  if (l <= tl && tr <= r) {
    return Mn[v];
  }
  int mid = (tl + tr) >> 1;
  return min(Get_mn(v << 1, tl, mid, l, r),
             Get_mn(v << 1 | 1, mid + 1, tr, l, r));
}
int G(int v, int tl, int tr, int l, int r, int lwr) {
  if (l > tr || r < tl) return 0;
  if (l <= tl && tr <= r) {
    return lwr;
  }
  int mid = (tl + tr) >> 1;
  int ans = G(v << 1, tl, mid, l, r, id[v][lwr]);
  ans += G(v << 1 | 1, mid + 1, tr, l, r, lwr - id[v][lwr]);
  return ans;
}
long long Find(int l, int r, int x) {
  int lwr = upper_bound((seg[1]).begin(), (seg[1]).end(), x) - seg[1].begin();
  return G(1, 1, m, l, r, lwr);
}
inline pair<int, int> calc(int ind, int sz) {
  int d = 0, up = rnk[LOG - 1][ind];
  while (up - d > 1) {
    int mid = (up + d) >> 1;
    if (Get_mn(1, 1, m, mid + 1, rnk[LOG - 1][ind]) >= sz) {
      up = mid;
    } else {
      d = mid;
    }
  }
  int Left = up;
  d = rnk[LOG - 1][ind], up = m + 1;
  while (up - d > 1) {
    int mid = (up + d) >> 1;
    if (Get_mn(1, 1, m, rnk[LOG - 1][ind] + 1, mid) >= sz) {
      d = mid;
    } else {
      up = mid;
    }
  }
  int right = d;
  return make_pair(Left, right);
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &A[i]);
  }
  for (int i = 1; i < n; i++) {
    B[i] = A[i + 1] - A[i];
    len[i] = (n - i);
  }
  B[n] = -2e9 - 10;
  for (int i = n + 1; i < 2 * n; i++) {
    B[i] = A[i - n] - A[i - n + 1];
    len[i] = 2 * n - i;
  }
  m = 2 * n - 1;
  printf("\n");
  Build();
  LAST[0] = -1;
  for (int i = 1; i <= m; i++) {
    LAST[i] = LAST[i - 1];
    if (P[i] > n) {
      LAST[i] = i;
    }
    ps[i] = ps[i - 1];
    if (P[i] < n) {
      ps[i]++;
    }
  }
  Bu(1, 1, m);
  int q;
  scanf("%d", &q);
  while (q--) {
    int l, r;
    scanf("%d %d", &l, &r);
    if (l == r) {
      printf("%d\n", n - 1);
      continue;
    }
    int sz = r - l;
    r--;
    pair<int, int> cu = calc(l, sz);
    if (LAST[cu.second] >= cu.first) {
      int anot = P[LAST[cu.second]] - n;
      pair<int, int> alan = calc(anot, sz);
      int tot = Find(alan.first, alan.second, 2e9 + 10) -
                Find(alan.first, alan.second, r + 1) +
                Find(alan.first, alan.second, l - sz - 1);
      printf("%d\n", tot);
    } else {
      printf("0\n");
      continue;
    }
  }
  return 0;
}