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 maxn = 200005, maxd = 20;
const int inf = 1000000000;
struct Tqry {
  int l, r, d, i;
  Tqry *p;
};
Tqry *head[maxn], buf[maxn * 2], *ptr = buf;
int h[maxn], s[maxn], w[maxn];
int sa[maxn], ra[maxn], hei[maxn];
int minh[maxd][maxn], ans[maxn];
int f[maxn];
int tn, n, m, q, tot;
bool sacmp(int *r, int a, int b, int l) {
  return r[a] == r[b] && r[a + l] == r[b + l];
}
void suffix(int *s, int *sa, int n, int m) {
  static int ta[maxn], tb[maxn], f[maxn];
  int i, j, l, p, *x = ta, *y = tb;
  memset(f, 0, sizeof(int) * (m + 1));
  for (i = 1; i <= n; ++i) x[i] = s[i];
  for (i = 1; i <= n; ++i) ++f[x[i]];
  for (i = 1; i <= m; ++i) f[i] += f[i - 1];
  for (i = n; i; --i) sa[f[x[i]]--] = i;
  for (l = 1, p = m; p < n; l *= 2, m = p) {
    for (i = n - l + 1, p = 0; i <= n; ++i) y[++p] = i;
    for (i = 1; i <= n; ++i)
      if (sa[i] > l) y[++p] = sa[i] - l;
    memset(f, 0, sizeof(int) * (m + 1));
    for (i = 1; i <= n; ++i) ++f[x[i]];
    for (i = 1; i <= m; ++i) f[i] += f[i - 1];
    for (i = n; i; --i) sa[f[x[y[i]]]--] = y[i];
    swap(x, y);
    p = 1;
    x[sa[1]] = 1;
    for (i = 2; i <= n; ++i) x[sa[i]] = sacmp(y, sa[i - 1], sa[i], l) ? p : ++p;
  }
  for (i = 1, l = 0; i < n; ++i) {
    for (j = sa[x[i] - 1]; s[i + l] == s[j + l]; ++l)
      ;
    hei[x[i]] = l > 0 ? l-- : 0;
  }
  for (i = 1; i <= n; ++i) ra[i] = x[i];
}
void init() {
  int i, j;
  scanf("%d", &tn);
  n = tn * 2;
  for (i = 1; i <= tn; ++i) scanf("%d", h + i);
  for (i = 1; i < tn; ++i) s[i] = h[i] - h[i + 1], s[i + tn] = -s[i];
  s[tn] = -inf;
  s[n] = -inf - 1;
  for (i = 0; i < n; ++i) w[i] = s[i + 1];
  sort(w, w + n);
  m = unique(w, w + n) - w;
  for (i = 1; i <= n; ++i) s[i] = lower_bound(w, w + m, s[i]) - w + 1;
  suffix(s, sa, n, m);
  for (i = 1; i <= n; ++i) minh[0][i] = hei[i];
  for (j = 1; (1 << j) <= n; ++j)
    for (i = 1; i <= n - (1 << j) + 1; ++i)
      minh[j][i] = min(minh[j - 1][i], minh[j - 1][i + (1 << (j - 1))]);
}
int getmin(int a, int b) {
  int k = 31 - __builtin_clz(b - a + 1);
  return min(minh[k][a], minh[k][b - (1 << k) + 1]);
}
int lbsearch(int x, int v) {
  int l, r, mid;
  l = 1;
  r = x;
  while (l < r) {
    mid = (l + r) >> 1;
    if (getmin(mid + 1, x) < v)
      l = mid + 1;
    else
      r = mid;
  }
  return l;
}
int rbsearch(int x, int v) {
  int l, r, mid;
  l = x;
  r = n;
  while (l < r) {
    mid = (l + r + 1) >> 1;
    if (getmin(x + 1, mid) < v)
      r = mid - 1;
    else
      l = mid;
  }
  return r;
}
void add(int x) {
  for (; x <= n; x += x & -x) ++f[x];
}
int get(int x) {
  int ans = 0;
  for (; x > 0; x -= x & -x) ans += f[x];
  return ans;
}
void work() {
  int i, l, r, lv, rv, t;
  scanf("%d", &q);
  for (i = 1; i <= q; ++i) {
    scanf("%d%d", &l, &r);
    if (l == r) {
      ans[i] = tn - 1;
      continue;
    }
    lv = lbsearch(ra[l], r - l);
    rv = rbsearch(ra[l], r - l);
    t = tn + l - r + l - 1;
    if (t > tn) {
      *ptr = (Tqry){lv - 1, rv, 1, i, head[t]};
      head[t] = ptr++;
    }
    t = tn + r;
    if (t < n) {
      *ptr = (Tqry){lv - 1, rv, -1, i, head[t]};
      head[t] = ptr++;
      *ptr = (Tqry){lv - 1, rv, 1, i, head[n]};
      head[n] = ptr++;
    }
  }
  for (i = tn + 1; i <= n; ++i) {
    add(ra[i]);
    for (Tqry *p = head[i]; p; p = p->p) {
      t = get(p->r) - get(p->l);
      ans[p->i] += p->d * t;
    }
  }
  for (i = 1; i <= q; ++i) printf("%d\n", ans[i]);
}
int main() {
  init();
  work();
  return 0;
}