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 long long N = 2e5 + 5;
const long long A = 1610612741;
const long long B = 1311322777;
long long a[N], h[N][3], dif[N], pw[N], tree[N], ans[N], rev[N];
pair<long long, long long> val[N];
long long n;
vector<pair<pair<long long, long long>, long long> > queL[N], queR[N];
struct rng {
  long long l, r;
} s[N];
long long sum(long long k) {
  long long s = 0;
  while (k >= 1) {
    s += tree[k];
    k -= k & -k;
  }
  return s;
}
void add(long long k, long long x) {
  while (k <= n) {
    tree[k] += x;
    k += k & -k;
  }
}
long long get(long long l, long long r, long long type) {
  return (h[r][type] - h[l - 1][type] * pw[r - l + 1] + B * B) % B;
}
long long find(rng p, rng q, long long flag) {
  long long u = p.l, v = q.l;
  long long L = 1, R = min(p.r - p.l + 1, q.r - q.l + 1);
  while (L < R) {
    long long mid = (L + R) >> 1;
    if (get(u, u + mid - 1, 0) == get(v, v + mid - 1, 0 ^ flag))
      L = mid + 1;
    else
      R = mid;
  }
  return L;
}
bool cmp(rng p, rng q) {
  long long L = find(p, q, 0ll);
  long long u = p.l, v = q.l;
  if (get(u, u + L - 1, 0) == get(v, v + L - 1, 0))
    return u > v;
  else
    return dif[u + L - 1] < dif[v + L - 1];
}
long long bsl(rng q) {
  long long L = 1, R = n, len = q.r - q.l + 1, u = q.l, v;
  while (L < R) {
    long long mid = (L + R) >> 1, v = s[mid].l;
    long long lef = find(s[mid], q, 1ll);
    if (dif[v + lef - 1] == rev[u + lef - 1]) {
      if (lef < len)
        L = mid + 1;
      else
        R = mid;
    } else {
      if (lef <= len && dif[v + lef - 1] < rev[u + lef - 1])
        L = mid + 1;
      else
        R = mid;
    }
  }
  v = s[L].l;
  long long tmp = min(n - u + 1, n - v + 1);
  if (tmp >= len && get(v, v + len - 1, 0) == get(u, u + len - 1, 1))
    return L;
  else
    return -1;
}
long long bsr(rng q) {
  long long L = 1, R = n, len = q.r - q.l + 1, u = q.l, v;
  while (L + 1 < R) {
    long long mid = (L + R) >> 1, v = s[mid].l;
    long long lef = find(s[mid], q, 1ll);
    if (lef <= len && dif[v + lef - 1] > rev[u + lef - 1])
      R = mid;
    else
      L = mid;
  }
  v = s[R].l;
  long long tmp = min(n - u + 1, n - v + 1);
  if (tmp >= len && get(v, v + len - 1, 0) == get(u, u + len - 1, 1))
    return R;
  else {
    v = s[L].l;
    tmp = min(n - u + 1, n - v + 1);
    if (tmp >= len && get(v, v + len - 1, 0) == get(u, u + len - 1, 1))
      return L;
    else
      return -1;
  }
}
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cin >> n;
  for (long long i = 1; i <= n; i++) {
    cin >> a[i];
  }
  pw[0] = 1;
  for (long long i = 1; i <= n; i++) {
    dif[i] = a[i] - a[i - 1];
    rev[i] = -dif[i];
    h[i][0] = (h[i - 1][0] * A + dif[i]) % B;
    h[i][1] = (h[i - 1][1] * A + rev[i]) % B;
    pw[i] = (pw[i - 1] * A) % B;
    s[i].l = i;
    s[i].r = n;
  }
  long long q;
  cin >> q;
  sort(s + 1, s + 1 + n, cmp);
  for (long long i = 1; i <= n; i++) {
    val[i].first = s[i].l;
    val[i].second = i;
  }
  for (long long i = 1; i <= q; i++) {
    long long l, r;
    cin >> l >> r;
    if (l == r) {
      ans[i] = n - 1;
    } else {
      l++;
      long long len = r - l + 1;
      rng p = {l, r};
      long long lef = bsl(p);
      long long rig = bsr(p);
      if (lef == -1 || rig == -1)
        ans[i] = 0;
      else {
        if (l - len - 1 >= 2) queL[l - len - 1].push_back({{lef, rig}, i});
        if (r + 2 <= n) queR[r + 2].push_back({{lef, rig}, i});
      }
    }
  }
  sort(val + 1, val + 1 + n);
  for (long long i = 1; i <= n; i++) {
    if (val[i].first > 1) {
      add(val[i].second, 1);
    }
    for (auto tmp : queL[val[i].first]) {
      long long L = tmp.first.first, R = tmp.first.second, id = tmp.second;
      ans[id] += sum(R) - sum(L - 1);
    }
  }
  memset(tree, 0, sizeof(tree));
  for (long long i = n; i >= 1; i--) {
    if (val[i].first > 1) {
      add(val[i].second, 1);
    }
    for (auto tmp : queR[val[i].first]) {
      long long L = tmp.first.first, R = tmp.first.second, id = tmp.second;
      ans[id] += sum(R) - sum(L - 1);
    }
  }
  for (long long i = 1; i <= q; i++) {
    cout << ans[i] << '\n';
  }
}