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 = 2e5 + 15;
const int Q = 20;
const int W = 333;
const int inf = 2e9 + N;
const int B = 131;
const int mod = 1e9 + 9;
int n, a[N], b[N];
int sab[N], rkb[N], lcp[N];
int nn, c[N + N], sac[N + N], rkc[N + N];
int hc[N + N], pwr[N + N], tl[N], tr[N];
int st[N][Q];
int m, l[N], r[N], len[N], ans[N], t[N];
pair<pair<int, int>, int> p[N + N];
int ql[N], qr[N], s[N];
vector<pair<pair<int, int>, int> > q[N];
int T[N + N];
int gethash(int L, int R) {
  long long res;
  res = (long long)pwr[nn - R + L - 1] * hc[R] % mod + mod;
  res -= (long long)pwr[nn] * hc[L - 1] % mod;
  return res % mod;
}
void ad(int x) {
  while (x <= N + n) {
    T[x]++;
    x += x & -x;
  }
}
int qu(int x) {
  int res;
  res = 0;
  while (x) {
    res += T[x];
    x -= x & -x;
  }
  return res;
}
int main() {
  int i, j, x, y, z, cl, cr;
  scanf("%d", &n);
  for (i = 1; i <= n; i = i + 1) {
    scanf("%d", a + i);
    if (i - 1) {
      b[i - 1] = a[i] - a[i - 1];
      c[i - 1] = b[i - 1];
      c[n + i - 1] = -b[i - 1];
    }
  }
  c[n] = -inf;
  for (i = 1; i < n; i = i + 1) p[i] = make_pair(make_pair(b[i], 0), i);
  for (j = 0; j < Q; j = j + 1) {
    x = 1 << j;
    sort(p + 1, p + n);
    for (i = 1; i < n; i = i + 1) {
      rkb[p[i].second] = rkb[p[i - 1].second];
      if (i == 1 || p[i].first != p[i - 1].first) rkb[p[i].second]++;
    }
    for (i = 1; i < n; i = i + 1) {
      p[i] = make_pair(make_pair(rkb[i], 0), i);
      if (i + x < n) p[i].first.second = rkb[i + x];
    }
  }
  for (i = 1; i < n; i = i + 1) sab[rkb[i]] = i;
  x = 0;
  for (i = 1; i < n; i = i + 1) {
    j = sab[rkb[i] - 1];
    while (j + x < n && i + x < n && b[j + x] == b[i + x]) {
      x++;
    }
    lcp[rkb[i] - 1] = x;
    if (x) x--;
  }
  for (i = 1; i < n - 1; i = i + 1) st[i][0] = lcp[i];
  x = 1;
  for (j = 1; j < Q; j = j + 1) {
    for (i = 1; i < n - 1; i = i + 1) {
      if (i + x >= n - 1)
        st[i][j] = 0;
      else
        st[i][j] = min(st[i][j - 1], st[i + x][j - 1]);
    }
    x <<= 1;
  }
  nn = n + n;
  for (i = 1; i < nn; i = i + 1) p[i] = make_pair(make_pair(c[i], 0), i);
  for (j = 0; j < Q; j = j + 1) {
    x = 1 << j;
    sort(p + 1, p + nn);
    for (i = 1; i < nn; i = i + 1) {
      rkc[p[i].second] = rkc[p[i - 1].second];
      if (i == 1 || p[i].first != p[i - 1].first) rkc[p[i].second]++;
    }
    for (i = 1; i < nn; i = i + 1) {
      p[i] = make_pair(make_pair(rkc[i], 0), i);
      if (i + x < nn) p[i].first.second = rkc[i + x];
    }
  }
  for (i = 1; i < nn; i = i + 1) sac[rkc[i]] = i;
  x = 0;
  for (i = 1; i < nn; i = i + 1) {
    j = sac[i];
    if (j == n) continue;
    if (j < n)
      x = j;
    else
      tl[j - n] = x;
  }
  x = 0;
  for (i = nn - 1; i > 0; i = i - 1) {
    j = sac[i];
    if (j == n) continue;
    if (j < n)
      x = j;
    else
      tr[j - n] = x;
  }
  scanf("%d", &m);
  for (i = 1; i <= m; i = i + 1) {
    scanf("%d%d", l + i, r + i);
    len[i] = r[i] - l[i];
    r[i]--;
    if (!len[i]) ans[i] = n - 1;
  }
  pwr[0] = 1;
  hc[0] = 0;
  for (i = 1; i <= nn; i = i + 1) {
    pwr[i] = (long long)pwr[i - 1] * B % mod;
    hc[i] = ((long long)hc[i - 1] * B + (long long)c[i] + (long long)mod * 10) %
            mod;
  }
  for (i = 1; i <= m; i = i + 1) {
    if (!len[i]) continue;
    if (tl[l[i]] + len[i] <= n &&
        gethash(l[i] + n, r[i] + n) == gethash(tl[l[i]], tl[l[i]] + len[i] - 1))
      t[i] = tl[l[i]];
    if (tr[l[i]] + len[i] <= n &&
        gethash(l[i] + n, r[i] + n) == gethash(tr[l[i]], tr[l[i]] + len[i] - 1))
      t[i] = tr[l[i]];
  }
  for (i = 1; i <= m; i = i + 1) {
    if (!t[i]) continue;
    ql[i] = rkb[t[i]];
    for (j = Q - 1; j >= 0; j = j - 1) {
      x = 1 << j;
      if (ql[i] - x > 0 && st[ql[i] - x][j] >= len[i]) ql[i] -= x;
    }
    qr[i] = rkb[t[i]];
    for (j = Q - 1; j >= 0; j = j - 1) {
      x = 1 << j;
      if (qr[i] + x < n && st[qr[i]][j] >= len[i]) qr[i] += x;
    }
    if (l[i] - len[i] - 1 > 0)
      q[l[i] - len[i] - 1].push_back(make_pair(make_pair(ql[i], qr[i]), i));
    q[r[i] + 1].push_back(make_pair(make_pair(ql[i], qr[i]), -i));
    q[n - len[i]].push_back(make_pair(make_pair(ql[i], qr[i]), i));
  }
  for (i = 1; i <= n; i = i + 1) {
    if (i < n) ad(rkb[i]);
    for (j = 0; j < q[i].size(); j = j + 1) {
      y = qu(q[i][j].first.second) - qu(q[i][j].first.first - 1);
      z = q[i][j].second;
      if (z > 0)
        ans[z] += y;
      else
        ans[-z] -= y;
    }
  }
  for (i = 1; i <= m; i = i + 1) cout << ans[i] << endl;
  return 0;
}