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 = (int)1e5, maxl = 18;
int a[maxn];
int ar[2 * maxn];
int allLetters[2 * maxn];
int s[2 * maxn];
int tmp[7][2 * maxn];
int *head = tmp[0], *cnt = tmp[1];
int *pos = tmp[2], *npos = tmp[3];
int *col = tmp[4], *ncol = tmp[5];
int sparse[maxl][2 * maxn];
int *lcp = sparse[0];
int prec[2 * maxn];
int MOD;
inline int mmod(int x) { return x >= MOD ? x - MOD : x; }
void buildSuffAr(int *s, int n, int curci) {
  memset(cnt, 0, sizeof(cnt[0]) * curci);
  for (int i = 0; i < n; i++) cnt[s[i]]++;
  head[0] = 0;
  for (int j = 1; j < curci; j++) head[j] = head[j - 1] + cnt[j - 1];
  for (int i = 0; i < n; i++) {
    col[i] = s[i];
    pos[head[col[i]]++] = i;
  }
  head[0] = 0;
  for (int j = 1; j < curci; j++) head[j] = head[j - 1] + cnt[j - 1];
  MOD = n;
  for (int len = 1; len <= n; len <<= 1) {
    for (int i = 0; i < n; i++) {
      int ic = pos[i] - len;
      if (ic < 0) ic += n;
      npos[head[col[ic]]++] = ic;
    }
    ncol[npos[0]] = 0;
    head[0] = 0;
    for (int i = 1; i < n; i++) {
      ncol[npos[i]] = ncol[npos[i - 1]];
      if (col[npos[i]] != col[npos[i - 1]] ||
          col[mmod(npos[i] + len)] != col[mmod(npos[i - 1] + len)])
        head[++ncol[npos[i]]] = i;
    }
    swap(ncol, col);
    swap(npos, pos);
    if (col[pos[n - 1]] == n - 1) break;
  }
  if (n < 100) {
    for (int i = 0; i < n - 1; i++) {
      for (int j = 0;; j++) {
        if (s[mmod(pos[i] + j)] != s[mmod(pos[i + 1] + j)]) {
          assert(s[mmod(pos[i] + j)] < s[mmod(pos[i + 1] + j)]);
          break;
        }
      }
    }
  }
  for (int i = 0, start = mmod(pos[n - 1] + 1), clcp = 0; i < n - 1;
       i++, clcp = max(0, clcp - 1)) {
    int cpos = mmod(start + i), nextcpos = pos[col[cpos] + 1];
    while (s[mmod(cpos + clcp)] == s[mmod(nextcpos + clcp)]) clcp++;
    assert(col[cpos] < n - 1);
    lcp[col[cpos]] = clcp;
  }
  if (n < 100) {
    for (int i = 0; i < n - 1; i++) {
      for (int j = 0;; j++) {
        if (s[mmod(pos[i] + j)] != s[mmod(pos[i + 1] + j)]) {
          assert(j == lcp[i]);
          break;
        }
      }
    }
  }
  prec[1] = 0;
  for (int i = 2; i <= n; i++) prec[i] = prec[i >> 1] + 1;
  for (int it = 1; it < maxl; it++) {
    for (int i = 0; i <= n - (1 << it); i++) {
      sparse[it][i] =
          min(sparse[it - 1][i], sparse[it - 1][i + (1 << (it - 1))]);
    }
  }
}
inline int countLcp(int l, int r) {
  if (l == r) return ((int)1E9);
  int x = prec[r - l];
  return min(sparse[x][l], sparse[x][r - (1 << x)]);
}
struct Node;
typedef Node *pNode;
struct Node {
  pNode l, r;
  int val;
  Node(pNode _l = 0, pNode _r = 0, int _val = 0) : l(_l), r(_r), val(_val) {}
};
pNode build(int n) {
  if (n == 1) return new Node(0, 0, 0);
  return new Node(build(n / 2), build((n + 1) / 2), 0);
}
pNode add(pNode v, int l, int r, int pos, int toadd) {
  if (!v) return 0;
  int mid = (l + r) / 2;
  pNode res = new Node(v->l, v->r, v->val);
  if (pos < mid)
    res->l = add(v->l, l, mid, pos, toadd);
  else
    res->r = add(v->r, mid, r, pos, toadd);
  res->val += toadd;
  assert(res);
  return res;
}
int get(pNode v, int l, int r, int l0, int r0) {
  if (l >= r0 || l0 >= r) return 0;
  if (l0 <= l && r <= r0) return v->val;
  assert(v);
  int mid = (l + r) / 2;
  return get(v->l, l, mid, l0, r0) + get(v->r, mid, r, l0, r0);
}
pNode ss[2 * maxn];
void init(int *ar, int n) {
  for (int i = 0; i < n; i++) {
    allLetters[i] = ar[i];
  }
  sort(allLetters, allLetters + n);
  int m = unique(allLetters, allLetters + n) - allLetters;
  for (int i = 0; i < n; i++) {
    s[i] = lower_bound(allLetters, allLetters + m, ar[i]) - allLetters + 1;
  }
  s[n] = 0;
  ++n;
  buildSuffAr(s, n, m + 1);
  ss[0] = build(n);
  assert(ss[0]);
  for (int i = 0; i < n; i++) {
    ss[i + 1] = add(ss[i], 0, n, pos[i], 1);
    assert(ss[i + 1]);
  }
}
int countleft(int pos, int len) {
  int l = 0, r = pos + 1;
  while (l + 1 < r) {
    int mid = (l + r) / 2;
    if (countLcp(pos - mid, pos) >= len)
      l = mid;
    else
      r = mid;
  }
  return pos - l;
}
int n;
int countright(int pos, int len) {
  int l = 0, r = 2 * n - 2 - pos + 1;
  while (l + 1 < r) {
    int mid = (l + r) / 2;
    if (countLcp(pos, pos + mid) >= len)
      l = mid;
    else
      r = mid;
  }
  return pos + l;
}
int mycount(int left, int right, int nomore) {
  return get(ss[right], 0, 2 * n - 1, 0, nomore + 1) -
         get(ss[left], 0, 2 * n - 1, 0, nomore + 1);
}
int solve(int l, int r) {
  int mid = col[l + n - 1];
  int left = countleft(mid, r - l);
  int right = countright(mid, r - l);
  assert(left >= 0 && countLcp(left, mid) >= r - l);
  assert(right <= 2 * n - 2 && countLcp(mid, right) >= r - l);
  right++;
  return mycount(left, right, n - (r - l + 1)) -
         mycount(left, right, min(n - (r - l + 1), r)) +
         mycount(left, right, l - (r - l + 1));
}
int main() {
  while (scanf("%d", &n) >= 1) {
    for (int i = 0; i < n; i++) {
      scanf("%d", &a[i]);
      if (i) ar[i - 1] = a[i] - a[i - 1];
    }
    for (int i = 0; i < n - 1; i++) ar[i + n - 1] = -ar[i];
    init(ar, 2 * n - 2);
    int m;
    scanf("%d", &m);
    for (int iter = 0; iter < m; iter++) {
      int l, r;
      scanf("%d%d", &l, &r);
      --l, --r;
      printf("%d\n", solve(l, r));
    }
  }
  return 0;
}