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 N = 2e5 + 10, LOG = 19, INF = 1e9 + 10;
int s[N], m, n;
int bu[LOG][N], sa[N], lcp[LOG][N], lg;
pair<pair<int, int>, int> tmp[N];
vector<int> seg[4 * N];
void build_suffix_array() {
  for (int i = (int)1; i <= n; i++) bu[0][i] = s[i];
  for (lg = 1; (1 << lg) <= 2 * n; lg++) {
    for (int i = (int)1; i <= n; i++)
      tmp[i] = {
          {bu[lg - 1][i],
           i + (1 << (lg - 1)) <= n ? bu[lg - 1][i + (1 << (lg - 1))] : -INF},
          i};
    sort(tmp + 1, tmp + n + 1);
    for (int i = (int)1; i <= n; i++)
      bu[lg][tmp[i].second] = (i > 1 && tmp[i].first == tmp[i - 1].first)
                                  ? bu[lg][tmp[i - 1].second]
                                  : i;
  }
  lg--;
  for (int i = (int)1; i <= n; i++) sa[bu[lg][i]] = i;
  int j, k = 0;
  for (int i = (int)1; i <= n; i++)
    if (bu[lg][i] != n) {
      j = sa[bu[lg][i] + 1];
      while (i + k <= n && j + k <= n && s[i + k] == s[j + k]) k++;
      lcp[0][bu[lg][i]] = k;
      if (k > 0) k--;
    }
  for (int j = (int)1; j <= lg; j++)
    for (int i = (int)1; i <= n - 1; i++)
      lcp[j][i] =
          min(lcp[j - 1][i],
              i + (1 << (j - 1)) < n ? lcp[j - 1][i + (1 << (j - 1))] : 0);
}
void build(int i, int lo, int hi) {
  if (lo == hi) {
    seg[i].push_back(sa[lo]);
    return;
  }
  int mid = lo + hi >> 1, c1 = i << 1, c2 = c1 | 1;
  build(c1, lo, mid);
  build(c2, mid + 1, hi);
  merge(seg[c1].begin(), seg[c1].end(), seg[c2].begin(), seg[c2].end(),
        back_inserter(seg[i]));
}
int ask(int i, int lo, int hi, int ql, int qr, int lowval, int hival) {
  if (hi < ql || qr < lo) return 0;
  if (ql <= lo && hi <= qr)
    return upper_bound(seg[i].begin(), seg[i].end(), hival) -
           lower_bound(seg[i].begin(), seg[i].end(), lowval);
  int mid = lo + hi >> 1, c1 = i << 1, c2 = c1 | 1;
  return ask(c1, lo, mid, ql, qr, lowval, hival) +
         ask(c2, mid + 1, hi, ql, qr, lowval, hival);
}
int query(int l, int r) {
  int pos = bu[lg][l], len = r - l;
  int lo = pos;
  for (int j = lg; j >= 0; j--)
    if (lo - (1 << j) > 0 && lcp[j][lo - (1 << j)] >= len) lo -= 1 << j;
  int hi = pos;
  for (int j = lg; j >= 0; j--)
    if (lcp[j][hi] >= len) hi += 1 << j;
  int res = 0;
  if (m + 1 <= m + l - (r - l) - 1)
    res += ask(1, 1, n, lo, hi, m + 1, m + l - (r - l) - 1);
  res += ask(1, 1, n, lo, hi, m + r + 1, n);
  return res;
}
int main() {
  scanf("%d", &m);
  for (int i = (int)1; i <= m; i++) scanf("%d", s + i);
  for (int i = (int)1; i <= m - 1; i++) s[i] -= s[i + 1];
  for (int i = (int)m + 1; i <= 2 * m - 1; i++) s[i] = -s[i - m];
  n = 2 * m - 1;
  build_suffix_array();
  build(1, 1, n);
  int l, r, q, ans;
  for (scanf("%d", &q); q--;) {
    scanf("%d%d", &l, &r);
    if (l == r)
      ans = m - 1;
    else
      ans = query(l, r);
    printf("%d\n", ans);
  }
  return 0;
}