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>
#pragma GCC optimize(2)
using namespace std;
void qread(int &x) {
  int neg = 1;
  x = 0;
  char c = getchar();
  while (c < '0' || c > '9') {
    if (c == '-') neg = -1;
    c = getchar();
  }
  while (c >= '0' && c <= '9') x = 10 * x + c - '0', c = getchar();
  x *= neg;
}
const int maxn = 200005;
const int mlog = 20;
int n, m, q, tot;
int a[maxn], dlt[maxn], ans[maxn];
class Event {
 public:
  int t, l, r, tp, id;
  Event() {}
  Event(int _t, int _l, int _r, int _tp, int _id)
      : t(_t), l(_l), r(_r), tp(_tp), id(_id) {}
  bool operator<(const Event &o) const { return t < o.t; }
} e[maxn];
namespace ST {
int mn[maxn][mlog], lg2[maxn];
void init(int *h) {
  lg2[0] = 0;
  for (int(i) = 1; (i) <= m; i++) {
    lg2[i] = lg2[i - 1];
    if (i == (1 << lg2[i] + 1)) lg2[i]++;
  }
  for (int(i) = 0; (i) < m; i++) mn[i][0] = h[i];
  for (int(j) = 1; (j) <= mlog - 1; j++)
    for (int(i) = 0; (i) < m; i++) {
      if (i + (1 << j - 1) >= m)
        mn[i][j] = 1e9;
      else
        mn[i][j] = min(mn[i][j - 1], mn[i + (1 << j - 1)][j - 1]);
    }
}
int ask(int l, int r) {
  r--;
  if (l > r) return 1e9;
  int len = lg2[r - l + 1];
  return min(mn[l][len], mn[r - (1 << len) + 1][len]);
}
}  // namespace ST
namespace BIT {
int t[maxn];
void init() {
  for (int(i) = 1; (i) <= m; i++) t[i] = 0;
}
void add(int v) {
  for (v++; v <= m; v += (v & -v)) t[v]++;
}
int sum(int v) {
  int res = 0;
  for (v++; v; v -= (v & -v)) res += t[v];
  return res;
}
}  // namespace BIT
namespace SA {
int sz;
int rk[maxn], sa[maxn], lcp[maxn], sum[maxn], tmp[maxn];
pair<int, int> cur[maxn];
map<int, int> num;
void rsort() {
  int mx = 0;
  for (int(i) = 0; (i) < m; i++)
    sum[cur[i].second]++, mx = max(mx, cur[i].second);
  for (int(i) = 1; (i) <= mx; i++) sum[i] += sum[i - 1];
  for (int i = m - 1; i >= 0; i--) tmp[--sum[cur[i].second]] = i;
  for (int(i) = 0; (i) < mx + 1; i++) sum[i] = 0;
  mx = 0;
  for (int(i) = 0; (i) < m; i++)
    sum[cur[tmp[i]].first]++, mx = max(mx, cur[tmp[i]].first);
  for (int(i) = 1; (i) <= mx; i++) sum[i] += sum[i - 1];
  for (int i = m - 1; i >= 0; i--) sa[--sum[cur[tmp[i]].first]] = tmp[i];
  for (int(i) = 0; (i) < mx + 1; i++) sum[i] = 0;
  rk[sa[0]] = 0;
  for (int(i) = 1; (i) <= m - 1; i++)
    rk[sa[i]] = rk[sa[i - 1]] + (cur[sa[i]] == cur[sa[i - 1]] ? 0 : 1);
}
void work(int *a) {
  for (int(i) = 0; (i) < m; i++) num[a[i]] = 0;
  sz = 0;
  for (map<int, int>::iterator it = num.begin(); it != num.end(); it++)
    it->second = sz++;
  for (int(i) = 0; (i) < m; i++)
    a[i] = num[a[i]], cur[i] = make_pair(a[i], a[i]);
  rsort();
  for (int k = 1; k <= m; k <<= 1) {
    for (int(i) = 0; (i) < m; i++)
      cur[i] = make_pair(rk[i] + 1, i + k >= m ? 0 : rk[i + k] + 1);
    rsort();
  }
  int h = 0;
  for (int(i) = 0; (i) < m; i++) {
    if (rk[i] == 0) continue;
    int j = sa[rk[i] - 1];
    if (h) h--;
    while (i + h < m && j + h < m && a[i + h] == a[j + h]) h++;
    lcp[rk[i] - 1] = h;
  }
}
}  // namespace SA
int main() {
  scanf("%d", &n);
  for (int(i) = 0; (i) < n; i++) scanf("%d", &a[i]);
  m = 2 * n - 2;
  for (int(i) = 0; (i) < n - 1; i++) {
    dlt[i] = a[i + 1] - a[i];
    dlt[i + n - 1] = -dlt[i];
  }
  SA::work(dlt);
  ST::init(SA::lcp);
  scanf("%d", &q);
  for (int(i) = 0; (i) < q; i++) {
    int ql, qr, cl, cr;
    scanf("%d%d", &ql, &qr);
    ql--;
    qr--;
    if (ql == qr) {
      ans[i] = n - 1;
      continue;
    }
    int l = -1, r = SA::rk[ql];
    while (r - l > 1) {
      int mid = (l + r) >> 1;
      if (ST::ask(mid, SA::rk[ql]) >= qr - ql)
        r = mid;
      else
        l = mid;
    }
    cl = r;
    l = SA::rk[ql];
    r = m;
    while (r - l > 1) {
      int mid = (l + r) >> 1;
      if (ST::ask(SA::rk[ql], mid) >= qr - ql)
        l = mid;
      else
        r = mid;
    }
    cr = l;
    e[tot++] = Event(cl - 1, ql, qr, -1, i);
    e[tot++] = Event(cr, ql, qr, 1, i);
  }
  sort(e, e + tot);
  int ptr = 0;
  for (int(i) = 0; (i) < tot; i++) {
    while (ptr < m && ptr <= e[i].t) {
      if (SA::sa[ptr] >= n - 1) BIT::add(SA::sa[ptr] - (n - 1));
      ptr++;
    }
    int ql = e[i].l, qr = e[i].r;
    ans[e[i].id] += e[i].tp * BIT::sum(max(-1, ql - (qr - ql + 1)));
    ans[e[i].id] += e[i].tp * (BIT::sum(n - 1) - BIT::sum(qr));
  }
  for (int(i) = 0; (i) < q; i++) printf("%d\n", ans[i]);
  return 0;
}