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 = 1e5 + 5;
const long long base = 1610612741;
const long long mod = 1311322777;
long long diff[N], rev[N], hasH[N][2], mu[N], b[N];
int n, q, a[N], l, r, ans[N], fen[N];
pair<int, int> val[N];
vector<pair<pair<int, int>, int> > queryL[N], queryR[N];
void update(int id, int val) {
  for (int i = id; i <= n; i += (i & -i)) {
    fen[i] += val;
  }
}
int get(int id) {
  int sum = 0;
  for (int i = id; i >= 1; i -= (i & -i)) {
    sum += fen[i];
  }
  return sum;
}
long long get_hash(int l, int r, int type) {
  return (hasH[r][type] - hasH[l - 1][type] * mu[r - l + 1] + mod * mod) % mod;
}
bool cmp(int a, int b) {
  int l = 1, r = min(n - a + 1, n - b + 1);
  while (l < r) {
    int mid = (l + r) >> 1;
    if (get_hash(a, a + mid - 1, 0) == get_hash(b, b + mid - 1, 0)) {
      l = mid + 1;
    } else {
      r = mid;
    }
  }
  if (get_hash(a, a + l - 1, 0) == get_hash(b, b + l - 1, 0)) {
    return a > b;
  } else {
    return diff[a + l - 1] < diff[b + l - 1];
  }
}
int searchL(int id, int len) {
  int l = 1, r = n;
  while (l < r) {
    int mid = (l + r) >> 1;
    int lef = 1, rig = min(n - id + 1, n - a[mid] + 1);
    while (lef < rig) {
      int md = (lef + rig) >> 1;
      if (get_hash(a[mid], a[mid] + md - 1, 0) ==
          get_hash(id, id + md - 1, 1)) {
        lef = md + 1;
      } else {
        rig = md;
      }
    }
    if (diff[a[mid] + lef - 1] == rev[id + lef - 1]) {
      if (lef < len) {
        l = mid + 1;
      } else {
        r = mid;
      }
    } else {
      if (lef <= len && diff[a[mid] + lef - 1] < rev[id + lef - 1]) {
        l = mid + 1;
      } else {
        r = mid;
      }
    }
  }
  int tmp = min(n - id + 1, n - a[l] + 1);
  if (tmp >= len &&
      get_hash(a[l], a[l] + len - 1, 0) == get_hash(id, id + len - 1, 1)) {
    return l;
  } else {
    return -1;
  }
}
int searchR(int id, int len) {
  int l = 1, r = n;
  while (l + 1 < r) {
    int mid = (l + r) >> 1;
    int lef = 1, rig = min(n - id + 1, n - a[mid] + 1);
    while (lef < rig) {
      int md = (lef + rig) >> 1;
      if (get_hash(a[mid], a[mid] + md - 1, 0) ==
          get_hash(id, id + md - 1, 1)) {
        lef = md + 1;
      } else {
        rig = md;
      }
    }
    if (lef <= len && diff[a[mid] + lef - 1] > rev[id + lef - 1]) {
      r = mid;
    } else {
      l = mid;
    }
  }
  int tmp = min(n - id + 1, n - a[r] + 1);
  if (tmp >= len &&
      get_hash(a[r], a[r] + len - 1, 0) == get_hash(id, id + len - 1, 1)) {
    return r;
  } else {
    int tmp = min(n - id + 1, n - a[l] + 1);
    if (tmp >= len &&
        get_hash(a[l], a[l] + len - 1, 0) == get_hash(id, id + len - 1, 1)) {
      return l;
    } else {
      return -1;
    }
  }
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    diff[i] = a[i] - a[i - 1];
    rev[i] = -diff[i];
    b[i] = a[i];
  }
  mu[0] = 1;
  for (int i = 1; i <= n; i++) {
    hasH[i][0] = (hasH[i - 1][0] * base + (diff[i] + mod * mod) % mod) % mod;
    hasH[i][1] = (hasH[i - 1][1] * base + (rev[i] + mod * mod) % mod) % mod;
    mu[i] = (mu[i - 1] * base) % mod;
    a[i] = i;
  }
  sort(a + 1, a + 1 + n, cmp);
  for (int i = 1; i <= n; i++) {
    val[i].first = a[i];
    val[i].second = i;
  }
  cin >> q;
  for (int i = 1; i <= q; i++) {
    cin >> l >> r;
    if (l == r) {
      ans[i] = n - 1;
    } else {
      l++;
      int len = r - l + 1;
      int lef = searchL(l, len);
      int rig = searchR(l, len);
      if (lef == -1 || rig == -1) {
        ans[i] = 0;
      } else {
        if (l - len - 1 >= 2) {
          queryL[l - len - 1].push_back({{lef, rig}, i});
        }
        if (r + 2 <= n) {
          queryR[r + 2].push_back({{lef, rig}, i});
        }
      }
    }
  }
  sort(val + 1, val + 1 + n);
  for (int i = 1; i <= n; i++) {
    if (val[i].first > 1) {
      update(val[i].second, 1);
    }
    for (auto tmp : queryL[val[i].first]) {
      int L = tmp.first.first, R = tmp.first.second, id = tmp.second;
      ans[id] += get(R) - get(L - 1);
    }
  }
  for (int i = 1; i <= n; i++) fen[i] = 0;
  for (int i = n; i >= 1; i--) {
    if (val[i].first > 1) {
      update(val[i].second, 1);
    }
    for (auto tmp : queryR[val[i].first]) {
      int L = tmp.first.first, R = tmp.first.second, id = tmp.second;
      ans[id] += get(R) - get(L - 1);
    }
  }
  for (int i = 1; i <= q; i++) {
    cout << ans[i] << "\n";
  }
}