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 = 3e5 + 5;
const long long inf = 1e9 + 5;
long long n, h[N], sA[2 * N], rB[2 * N], G, rA[2 * N], lcp[2 * N], ans[N],
    pos[2 * N], tmp[2 * N];
long long pw[2 * N], rmq[25][2 * N], mylog[2 * N], bit[2 * N], hs[2 * N], m, q;
const long long base = 1610612741;
const long long mod = 1311322777;
long long add(long long x, long long y) { return (1ll * x + 1ll * y) % mod; }
long long del(long long x, long long y) {
  return ((1ll * x - 1ll * y) % mod + mod) % mod;
}
long long mul(long long x, long long y) { return (1ll * x * 1ll * y) % mod; }
long long get(long long pos) {
  long long sum = 0;
  while (pos > 0) {
    sum += bit[pos];
    pos -= pos & (-pos);
  }
  return sum;
}
void up(long long pos, long long val) {
  while (pos < 2 * N) {
    bit[pos] += val;
    pos += pos & (-pos);
  }
}
bool cmp(long long x, long long y) {
  if (rA[x] != rA[y]) return rA[x] < rA[y];
  return rA[x + G] < rA[y + G];
}
long long geths(long long l, long long r) {
  return del(hs[r], mul(hs[l - 1], pw[r - l + 1]));
}
long long get(long long x, long long y) {
  long long l = 1, r = m - max(x, y) + 1, md;
  while (l < r) {
    long long md = (l + r + 1) >> 1;
    if (geths(x, x + md - 1) == geths(y, y + md - 1))
      l = md;
    else
      r = md - 1;
  }
  if (geths(x, x + l - 1) == geths(y, y + l - 1)) return l;
  return 0;
}
long long getrmq(long long x, long long y) {
  if (x > y) return inf;
  long long bit = mylog[y - x + 1];
  return min(rmq[bit][x], rmq[bit][y - (1ll << bit) + 1]);
}
vector<pair<pair<long long, long long>, pair<long long, long long> > > query;
vector<pair<long long, long long> > qu, vec;
signed main() {
  cin.tie(0), ios_base::sync_with_stdio(0);
  pw[0] = 1;
  for (long long i = 0; i < 2 * N; i++) mylog[i] = log2(i);
  for (long long i = 1; i < 2 * N; i++) pw[i] = mul(pw[i - 1], base);
  cin >> n;
  for (long long i = 1; i <= n; i++) cin >> h[i];
  for (long long i = 1; i <= n - 1; i++) sA[i] = i, rA[i] = h[i + 1] - h[i];
  sA[n] = n, rA[n] = INT_MAX;
  m = 2 * n - 1;
  for (long long i = n + 1; i <= m; i++)
    sA[i] = i, rA[i] = h[i - n] - h[i - n + 1];
  for (long long i = 1; i <= m; i++) hs[i] = add(mul(hs[i - 1], base), rA[i]);
  for (long long i = m + 1; i < 3 * N; i++) rA[i] = INT_MAX;
  for (G = 1; G <= m; G *= 2) {
    sort(sA + 1, sA + 1 + m, cmp);
    for (long long i = 1; i <= m; i++)
      rB[sA[i]] = rB[sA[i - 1]] + cmp(sA[i - 1], sA[i]);
    for (long long i = 1; i <= m; i++) rA[i] = rB[i];
    if (rA[sA[m]] == m) break;
  }
  for (long long i = 1; i <= m; i++) pos[sA[i]] = i;
  for (long long i = 1; i <= m - 1; i++) {
    lcp[i] = get(sA[i], sA[i + 1]);
    rmq[0][i] = lcp[i];
  }
  for (long long bit = 1; bit < 25; bit++) {
    for (long long i = 1; i <= m - 1; i++) {
      long long j = i + (1ll << (bit - 1));
      if (j > m - 1) break;
      rmq[bit][i] = min(rmq[bit - 1][i], rmq[bit - 1][j]);
    }
  }
  cin >> q;
  for (long long i = 1; i <= q; i++) {
    long long l, r, len;
    cin >> l >> r;
    len = r - l + 1;
    qu.push_back(make_pair(l, r));
    if (l == r)
      ans[i] = n - 1;
    else
      query.push_back(make_pair(make_pair(min(n + l - len, m + 1), len - 1),
                                make_pair(l, i)));
  }
  sort(query.begin(), query.end());
  long long po = n + 1;
  for (long long i = 0; i < (long long)(query.size()); i++) {
    long long le = query[i].first.first, li = query[i].second.first,
              id = query[i].second.second, len = query[i].first.second;
    while (po <= le) {
      up(pos[po], 1), po++;
    }
    long long Left, Right, l = 1, r = pos[li], md;
    while (l < r) {
      long long md = (l + r) >> 1;
      if (getrmq(md, pos[li] - 1) >= len)
        r = md;
      else
        l = md + 1;
    }
    Left = l;
    l = pos[li], r = m;
    while (l < r) {
      long long md = (l + r + 1) >> 1;
      if (getrmq(pos[li], md - 1) >= len)
        l = md;
      else
        r = md - 1;
    }
    Right = l;
    if (Left <= Right) ans[id] += get(Right) - get(Left - 1);
  }
  query.clear();
  for (long long i = 1; i <= q; i++) {
    long long l, r;
    l = qu[i - 1].first, r = qu[i - 1].second;
    if (l == r) continue;
    query.push_back(
        make_pair(make_pair(min(n + 1 + r, m + 1), r - l), make_pair(l, i)));
  }
  memset(bit, 0, sizeof(bit));
  sort(query.begin(), query.end());
  po = m;
  for (long long i = (long long)(query.size()) - 1; i >= 0; i--) {
    long long le = query[i].first.first, li = query[i].second.first,
              id = query[i].second.second, len = query[i].first.second;
    while (po >= le) {
      up(pos[po], 1), po--;
    }
    long long Left, Right, l = 1, r = pos[li], md;
    while (l < r) {
      long long md = (l + r) >> 1;
      if (getrmq(md, pos[li] - 1) >= len)
        r = md;
      else
        l = md + 1;
    }
    Left = l;
    l = pos[li], r = m;
    while (l < r) {
      long long md = (l + r + 1) >> 1;
      if (getrmq(pos[li], md - 1) >= len)
        l = md;
      else
        r = md - 1;
    }
    Right = l;
    if (Left <= Right) ans[id] += get(Right) - get(Left - 1);
  }
  for (long long i = 1; i <= q; i++) cout << ans[i] << "\n";
}