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;
int n, tot;
int b[400005], s[400005], sa[400005], ra[400005], height[400005], nue[400005],
    sum[400005];
int st[400005][25], tree[400005], lc[400005 * 40], rc[400005 * 40],
    a[400005 * 40];
bool cmp(int x, int y) {
  if (s[x] != s[y]) return s[x] < s[y];
  return x < y;
}
void build_sa() {
  n = n * 2 - 1;
  for (int i = 1; i <= n; i++) sa[i] = i;
  sort(sa + 1, sa + n + 1, cmp);
  ra[sa[1]] = 1;
  for (int i = 2; i <= n; i++) {
    if (s[sa[i]] == s[sa[i - 1]]) {
      ra[sa[i]] = ra[sa[i - 1]];
    } else {
      ra[sa[i]] = ra[sa[i - 1]] + 1;
    }
  }
  for (int k = 1; k <= n; k <<= 1) {
    int ge = 0;
    for (int i = n - k + 1; i <= n; i++) nue[++ge] = i;
    for (int i = 1; i <= n; i++) {
      sum[i] = 0;
      if (sa[i] > k) nue[++ge] = sa[i] - k;
    }
    for (int i = 1; i <= n; i++) sum[ra[i]]++;
    for (int i = 2; i <= n; i++) sum[i] += sum[i - 1];
    for (int i = n; i; i--) {
      sa[sum[ra[nue[i]]]--] = nue[i];
    }
    swap(ra, nue);
    ra[sa[1]] = 1;
    for (int i = 2; i <= n; i++) {
      if (nue[sa[i]] > nue[sa[i - 1]] || sa[i - 1] + k > n ||
          nue[sa[i] + k] != nue[sa[i - 1] + k]) {
        ra[sa[i]] = ra[sa[i - 1]] + 1;
      } else {
        ra[sa[i]] = ra[sa[i - 1]];
      }
    }
  }
  int h = 0;
  for (int i = 1; i <= n; i++) {
    if (ra[i] == 1) continue;
    if (h) h--;
    int j = sa[ra[i] - 1];
    while (j + h <= n && i + h <= n && s[j + h] == s[i + h]) h++;
    height[ra[i]] = h;
  }
}
int insert(int x, int y, int l, int r, int wh) {
  x = ++tot;
  a[x] = a[y] + 1;
  if (l == r) {
    return x;
  }
  int mid = (l + r) / 2;
  if (mid >= wh) lc[x] = insert(lc[x], lc[y], l, mid, wh), rc[x] = rc[y];
  if (mid < wh) rc[x] = insert(rc[x], rc[y], mid + 1, r, wh), lc[x] = lc[y];
  return x;
}
int query(int x, int y, int l, int r, int tx, int ty) {
  if (!x && !y) return 0;
  if (tx > ty) return 0;
  if (l >= tx && r <= ty) return a[y] - a[x];
  int mid = (l + r) / 2, ret = 0;
  if (mid >= tx) ret += query(lc[x], lc[y], l, mid, tx, ty);
  if (mid < ty) ret += query(rc[x], rc[y], mid + 1, r, tx, ty);
  return ret;
}
int calc(int l, int r) {
  if (l > r) swap(l, r);
  int t = 0;
  l++;
  if (l == r) return height[r];
  for (; l + (1 << t) < r; t++)
    ;
  if (l + (1 << t) > r) t--;
  return min(st[l][t], st[r - (1 << t) + 1][t]);
}
int find(int s, int x, int op) {
  int l, r, m;
  if (op)
    l = s, r = n;
  else
    l = 1, r = s;
  while (l != r) {
    m = (l + r) >> 1;
    if (op) m++;
    if (calc(m, s) < x) {
      if (op)
        r = m - 1;
      else
        l = m + 1;
    } else {
      if (op)
        l = m;
      else
        r = m;
    }
  }
  return l;
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) scanf("%d", &b[i]);
  for (int i = 1; i < n; i++) s[i] = b[i + 1] - b[i];
  s[n] = 1;
  for (int i = 1; i < n; i++) s[n + i] = -s[i];
  build_sa();
  for (int i = 1; i < n * 2; i++) st[i][0] = height[i];
  for (int j = 1; j <= 20; j++) {
    for (int i = 1; i < n * 2; i++) {
      st[i][j] = st[i][j - 1];
      if (i + (1 << (j - 1)) <= n)
        st[i][j] = min(st[i][j], st[i + (1 << (j - 1))][j - 1]);
    }
  }
  tree[0] = 1;
  for (int i = 1; i <= n; i++) {
    tree[i] = insert(tree[i], tree[i - 1], 1, n, sa[i]);
  }
  int N = (n + 1) / 2;
  int Q;
  scanf("%d", &Q);
  while (Q--) {
    int x, y;
    scanf("%d%d", &x, &y);
    if (x == y) {
      printf("%d\n", N - 1);
      continue;
    }
    int l = x, r = y;
    x = find(ra[l], r - l, 0), y = find(ra[l], r - l, 1);
    printf("%d\n",
           query(tree[x - 1], tree[y], 1, n, N + 1, N + (l - 1) - (r - l)) +
               query(tree[x - 1], tree[y], 1, n, N + (r + 1), n));
  }
  return 0;
}