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 = 2e5 + 100;
struct Node {
  int l, r;
  int num;
} tree[maxN * 25 + 1];
int n, m, p, q, len;
int sa[maxN + 1], rk[maxN + 1], bin[maxN + 1], tp[maxN + 1];
int h[maxN + 1], st[maxN + 1][21], lg[maxN + 1];
int s[maxN + 1], tmp[maxN + 1];
int root[maxN + 1], cnt;
inline int read() {
  int num = 0, f = 1;
  char ch = getchar();
  while (!isdigit(ch)) {
    if (ch == '-') f = -1;
    ch = getchar();
  }
  while (isdigit(ch)) num = (num << 3) + (num << 1) + (ch ^ 48), ch = getchar();
  return num * f;
}
inline void print(int x) {
  if (x < 10) {
    putchar(x + '0');
    return;
  }
  print(x / 10);
  putchar(x % 10 + '0');
}
inline void Qsort() {
  for (int i = 0; i <= m; i++) bin[i] = 0;
  for (int i = 1; i <= len; i++) bin[rk[i]]++;
  for (int i = 1; i <= m; i++) bin[i] += bin[i - 1];
  for (int i = len; i >= 1; i--) sa[bin[rk[tp[i]]]--] = tp[i];
}
inline void get_sa() {
  for (int i = 1; i <= len; i++) rk[i] = s[i], tp[i] = i;
  Qsort();
  for (int w = 1; p < len; m = p, w <<= 1) {
    p = 0;
    for (int i = len - w + 1; i <= len; i++) tp[++p] = i;
    for (int i = 1; i <= len; i++)
      if (sa[i] > w) tp[++p] = sa[i] - w;
    Qsort();
    memcpy(tp, rk, sizeof(rk));
    rk[sa[1]] = p = 1;
    for (int i = 2; i <= len; i++)
      rk[sa[i]] =
          (tp[sa[i]] == tp[sa[i - 1]] && tp[sa[i] + w] == tp[sa[i - 1] + w])
              ? p
              : ++p;
  }
}
inline void get_height() {
  int k = 0;
  for (int i = 1; i <= len; i++) {
    if (k) k--;
    int j = sa[rk[i] - 1];
    while (s[i + k] == s[j + k]) k++;
    h[rk[i]] = k;
  }
}
inline void pushup(int node) {
  tree[node].num = tree[tree[node].l].num + tree[tree[node].r].num;
}
inline int build(int l, int r) {
  int node = ++cnt;
  if (l == r) return node;
  int mid = (l + r) >> 1;
  tree[node].l = build(l, mid);
  tree[node].r = build(mid + 1, r);
  pushup(node);
  return node;
}
inline int change(int pre, int l, int r, int x) {
  int node = ++cnt;
  if (l == r) {
    tree[node].num = tree[pre].num + 1;
    return node;
  }
  int mid = (l + r) >> 1;
  tree[node].l = tree[pre].l;
  tree[node].r = tree[pre].r;
  if (x <= mid)
    tree[node].l = change(tree[pre].l, l, mid, x);
  else
    tree[node].r = change(tree[pre].r, mid + 1, r, x);
  pushup(node);
  return node;
}
inline int query(int u, int v, int l, int r, int x, int y) {
  if (x <= l && r <= y) return tree[v].num - tree[u].num;
  int mid = (l + r) >> 1, ans = 0;
  if (x <= mid) ans += query(tree[u].l, tree[v].l, l, mid, x, y);
  if (y > mid) ans += query(tree[u].r, tree[v].r, mid + 1, r, x, y);
  return ans;
}
inline int query_mn(int l, int r) {
  int k = lg[r - l + 1];
  return min(st[l][k], st[r - (1 << k) + 1][k]);
}
inline int solve(int x, int y) {
  if (x > y) return n - 1;
  int l = 1, r = rk[x] - 1, L = rk[x], R = rk[x], t = y - x + 1;
  while (l <= r) {
    int mid = (l + r) >> 1;
    if (query_mn(mid + 1, rk[x]) >= t)
      L = mid, r = mid - 1;
    else
      l = mid + 1;
  }
  l = rk[x] + 1, r = len;
  while (l <= r) {
    int mid = (l + r) >> 1;
    if (query_mn(rk[x] + 1, mid) >= t)
      R = mid, l = mid + 1;
    else
      r = mid - 1;
  }
  int ans = 0;
  if (x - t - 1 >= 2)
    ans += query(root[L - 1], root[R], 1, len, n + 3, x + n - t);
  if (y + n + 3 <= len)
    ans += query(root[L - 1], root[R], 1, len, y + n + 3, len);
  return ans;
}
int main() {
  n = read();
  int last = 0, x;
  for (int i = 1; i <= n; i++) x = read(), s[i] = tmp[i] = x - last, last = x;
  len = 2 * n + 1;
  for (int i = n + 2; i <= len; i++) s[i] = tmp[i] = -s[i - n - 1];
  s[n + 1] = tmp[n + 1] = -2e9 - 1;
  sort(tmp + 1, tmp + len + 1);
  m = unique(tmp + 1, tmp + len + 1) - tmp - 1;
  for (int i = 1; i <= len; i++)
    s[i] = lower_bound(tmp + 1, tmp + m + 1, s[i]) - tmp;
  get_sa();
  get_height();
  for (int i = 2; i <= len; i++) lg[i] = lg[i >> 1] + 1;
  for (int i = 1; i <= len; i++) st[i][0] = h[i];
  for (int j = 1; (1 << j) <= len; j++)
    for (int i = 1; i + (1 << j) - 1 <= len; i++)
      st[i][j] = min(st[i][j - 1], st[i + (1 << j - 1)][j - 1]);
  root[0] = build(1, len);
  for (int i = 1; i <= len; i++) root[i] = change(root[i - 1], 1, len, sa[i]);
  q = read();
  while (q--) {
    int l = read(), r = read();
    print(solve(l + 1, r));
    puts("");
  }
  return 0;
}