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;
inline void OPEN(const string &s) {
  freopen((s + ".in").c_str(), "r", stdin);
  freopen((s + ".out").c_str(), "w", stdout);
}
template <class T>
inline T MIN(T a, T b) {
  if (a < b) return a;
  return b;
}
template <class T>
inline T MAX(T a, T b) {
  if (a > b) return a;
  return b;
}
int second[200000 + 5];
int n;
int gap;
int sa[200000], pos[200000], tmp[200000], lcp[200000];
bool sufCmp(int i, int j) {
  if (pos[i] != pos[j]) return pos[i] < pos[j];
  i += gap;
  j += gap;
  return (i < n && j < n) ? pos[i] < pos[j] : i > j;
}
void buildSA() {
  for (int i = 0, _n = (n); i < _n; i++) sa[i] = i, pos[i] = second[i];
  for (gap = 1;; gap <<= 1) {
    sort(sa, sa + n, sufCmp);
    for (int i = 0, _n = (n - 1); i < _n; i++)
      tmp[i + 1] = tmp[i] + sufCmp(sa[i], sa[i + 1]);
    for (int i = 0, _n = (n); i < _n; i++) pos[sa[i]] = tmp[i];
    if (tmp[n - 1] == n - 1) break;
  }
}
void buildLCP() {
  for (int i = 0, k = 0; i < n; ++i)
    if (pos[i] != n - 1) {
      for (int j = sa[pos[i] + 1]; second[i + k] == second[j + k];) ++k;
      lcp[pos[i]] = k;
      if (k) --k;
    }
}
int H[200000 + 5];
vector<pair<pair<int, int>, int> > intervals;
int Q;
int ans[200000 + 5];
int BIT[200000 + 5];
int where[200000 + 5], cor[200000 + 5];
int RMQ[20][200000 + 5];
void initRMQ() {
  for (int lg = 0, _n = (20); lg < _n; lg++)
    for (int i = 0, _n = (n); i < _n; i++) RMQ[lg][i] = 1000111000;
  for (int i = 0, _n = (n); i < _n; i++) RMQ[0][i] = lcp[i];
  for (int lg = 0, _n = (20); lg < _n; lg++) {
    if (lg == 0) continue;
    for (int i = 0, _n = (n); i < _n; i++) {
      if (i + (1 << lg) - 1 < n) {
        RMQ[lg][i] = MIN(RMQ[lg - 1][i], RMQ[lg - 1][i + (1 << (lg - 1))]);
      }
    }
  }
}
int query(int l, int r) {
  int x = r - l + 1;
  int lg = 0;
  for (; (1 << lg) <= x; lg++)
    ;
  lg--;
  return MIN(RMQ[lg][l], RMQ[lg][r - (1 << lg) + 1]);
}
inline void add(int x) {
  for (++x; x <= 200000; x += x & -x) BIT[x]++;
}
inline int queryBIT(int x) {
  int ret = 0;
  for (++x; x > 0; x -= x & -x) ret += BIT[x];
  return ret;
}
inline int b_search(int i, int k) {
  k--;
  if (lcp[i] < k) return i;
  int lo = i, hi = n - 1;
  while (lo <= hi) {
    int mid = (lo + hi) >> 1;
    int q1 = query(i, mid);
    if (q1 >= k && (mid + 1 >= n || query(i, mid + 1) < k)) {
      return mid + 1;
    }
    if (q1 >= k)
      lo = mid + 1;
    else
      hi = mid - 1;
  }
}
inline int b_search_atas(int i, int k) {
  k--;
  if (i == 0 || lcp[i - 1] < k) return i;
  int lo = 0, hi = i - 1;
  while (lo <= hi) {
    int mid = (lo + hi) >> 1;
    int q1 = query(mid, i - 1);
    if (q1 >= k && (mid - 1 < 0 || query(mid - 1, i - 1) < k)) {
      return mid;
    }
    if (q1 >= k)
      hi = mid - 1;
    else
      lo = mid + 1;
  }
}
int N;
inline void solve() {
  n = 0;
  for (int i = 0, _n = (N - 1); i < _n; i++) second[n++] = H[i + 1] - H[i];
  for (int i = 0, _n = (N - 1); i < _n; i++) second[n++] = H[i] - H[i + 1];
  int mn = 1000111000;
  for (int i = 0, _n = (n); i < _n; i++) mn = MIN(mn, second[i]);
  for (int i = 0, _n = (n); i < _n; i++) second[i] = second[i] - mn + 1;
  buildSA();
  buildLCP();
  int n2 = n / 2;
  for (int i = 0, _n = (n); i < _n; i++) {
    if (sa[i] >= n2)
      where[sa[i] - n2] = i;
    else
      cor[sa[i]] = i;
  }
  for (int i = 0, _n = (Q); i < _n; i++)
    swap(intervals[i].first.first, intervals[i].first.second);
  sort((intervals).begin(), (intervals).end());
  for (int i = 0, _n = (Q); i < _n; i++)
    swap(intervals[i].first.first, intervals[i].first.second);
  initRMQ();
  memset(BIT, 0, sizeof(BIT));
  int j = n2 - 1;
  for (int i = (Q)-1; i >= 0; i--) {
    while (j > intervals[i].first.second) {
      add(where[j]);
      j--;
    }
    int ctr = intervals[i].first.second - intervals[i].first.first + 1;
    int k = b_search(cor[intervals[i].first.first], ctr);
    int p = b_search_atas(cor[intervals[i].first.first], ctr);
    ans[intervals[i].second] += queryBIT(k) - queryBIT(p - 1);
  }
}
int main() {
  scanf("%d", &N);
  for (int i = 0, _n = (N); i < _n; i++) scanf("%d", &H[i]);
  scanf("%d", &Q);
  if (N == 1) {
    for (int i = 0, _n = (Q); i < _n; i++) printf("0\n");
    return 0;
  }
  for (int i = 0, _n = (Q); i < _n; i++) {
    int a, b;
    scanf("%d%d", &a, &b);
    a--;
    b--;
    intervals.push_back(make_pair(make_pair(a, b), i));
  }
  solve();
  reverse(H, H + N);
  for (int i = 0, _n = (Q); i < _n; i++) {
    swap(intervals[i].first.first, intervals[i].first.second);
    intervals[i].first.first = N - 1 - intervals[i].first.first;
    intervals[i].first.second = N - 1 - intervals[i].first.second;
  }
  solve();
  for (int i = 0, _n = (Q); i < _n; i++) {
    if (intervals[i].first.first == intervals[i].first.second)
      ans[intervals[i].second] = N - 1;
  }
  for (int i = 0, _n = (Q); i < _n; i++) {
    printf("%d\n", ans[i]);
  }
  return 0;
}