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;
#pragma GCC optimize("Ofast")
const long long maxn = 3e5 + 10, N = 8e2 + 10, SQ = 320, base = 800287,
                mod = 1e9 + 7, INF = 1e18 + 10, lg = 22;
const long double eps = 1e-4;
long long n, m, q, a[maxn], b[maxn], fen[maxn];
long long Rank[maxn][lg], SA[maxn], pos[maxn], res[maxn];
vector<pair<long long,
            pair<pair<long long, long long>, pair<long long, long long>>>>
    query;
vector<pair<long long, pair<long long, long long>>> vec;
bool cmp(pair<long long, pair<long long, long long>> a,
         pair<long long, pair<long long, long long>> b) {
  if (a.second.first != b.second.first) {
    return a.second.first < b.second.first;
  }
  return a.second.second < b.second.second;
}
bool cmp2(pair<long long,
               pair<pair<long long, long long>, pair<long long, long long>>>
              a,
          pair<long long,
               pair<pair<long long, long long>, pair<long long, long long>>>
              b) {
  return a.second.second.second < b.second.second.second;
}
long long lcp(long long x, long long y) {
  long long res = 0;
  for (long long i = lg - 1; i >= 0; --i) {
    if (Rank[x][i] == Rank[y][i]) {
      res += (1ll << i);
      x += (1ll << i), y += (1ll << i);
    }
  }
  return res;
}
void build_suffix_array() {
  for (long long i = 0; i < m; ++i) {
    vec.push_back({i, {b[i], -1}});
  }
  sort(vec.begin(), vec.end(), cmp);
  long long id = 0;
  for (long long i = 0; i < m; ++i) {
    if (i && vec[i].second != vec[i - 1].second) {
      id++;
    }
    Rank[vec[i].first][0] = id;
  }
  for (long long i = 1; i < lg; ++i) {
    vec.clear();
    for (long long j = 0; j < m; ++j) {
      long long val =
          (j + (1ll << (i - 1)) < m ? Rank[j + (1ll << (i - 1))][i - 1] : -INF);
      vec.push_back({j, {Rank[j][i - 1], val}});
    }
    sort(vec.begin(), vec.end(), cmp);
    long long id = 0;
    for (long long j = 0; j < m; ++j) {
      if (j && vec[j].second != vec[j - 1].second) {
        id++;
      }
      Rank[vec[j].first][i] = id;
    }
  }
  for (long long i = 0; i < m; ++i) {
    SA[Rank[i][lg - 1]] = i;
  }
  for (long long i = 0; i < m; ++i) {
    pos[SA[i]] = i;
  }
}
long long bsL(long long l, long long r, long long k) {
  while (l < r - 1) {
    long long mm = (l + r) / 2, val = lcp(SA[r], SA[mm]);
    if (val >= k) {
      r = mm;
    } else {
      l = mm;
    }
  }
  return r;
}
long long bsR(long long l, long long r, long long k) {
  while (l < r - 1) {
    long long mm = (l + r) / 2, val = lcp(SA[l], SA[mm]);
    if (val >= k) {
      l = mm;
    } else {
      r = mm;
    }
  }
  return l;
}
void add(long long p) {
  for (p++; p < maxn; p += (p & -p)) {
    fen[p]++;
  }
}
long long get(long long p) {
  long long res = 0;
  for (; p; p -= (p & -p)) {
    res += fen[p];
  }
  return res;
}
int32_t main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cin >> n;
  for (long long i = 0; i < n; ++i) {
    cin >> a[i];
  }
  for (long long i = 0; i < n - 1; ++i) {
    b[i] = a[i + 1] - a[i];
    b[n + i] = -b[i];
  }
  b[n - 1] = INF, m = 2 * n - 1;
  build_suffix_array();
  cin >> q;
  for (long long i = 0; i < q; ++i) {
    long long l, r;
    cin >> l >> r;
    if (l == r) {
      res[i] = n - 1;
      continue;
    }
    l--, r--;
    long long p1 = bsL(-1, pos[l], r - l), p2 = bsR(pos[l], m, r - l) + 1;
    query.push_back({i, {{p1, p2}, {-1ll, r + n}}});
    query.push_back({i, {{p1, p2}, {1ll, max(0ll, 2 * l - r) + n - 1}}});
    query.push_back({i, {{p1, p2}, {-1ll, n - 1}}});
    res[i] = p2 - p1;
  }
  sort(query.begin(), query.end(), cmp2);
  long long x = 0;
  for (auto u : query) {
    long long lim = u.second.second.second, type = u.second.second.first,
              l = u.second.first.first, r = u.second.first.second, id = u.first;
    while (x < m && x <= lim) {
      add(pos[x]), x++;
    }
    res[id] += type * (get(r) - get(l));
  }
  for (long long i = 0; i < q; ++i) {
    cout << res[i] << '\n';
  }
}