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;
template <typename T>
int suffix_array_build_rank(int N, const int suffix[], const T equality[],
                            int new_rank[], int len) {
  int cur_rank = 0;
  int base = suffix[cur_rank];
  new_rank[base] = cur_rank;
  for (int i = 1; i < N; ++i) {
    int cur = suffix[i];
    if (!(base + len < N && cur + len < N && equality[cur] == equality[base] &&
          equality[cur + len] == equality[base + len])) {
      ++cur_rank;
      base = cur;
    }
    new_rank[cur] = cur_rank;
  }
  return ++cur_rank;
}
template <typename T>
void build_suffix_array(int N, T str[], int suffix[], int suffix_rank[],
                        int lcp[], int tmp[]) {
  for (int i = 0; i < N; i++) {
    suffix[i] = i;
  }
  sort(suffix, suffix + N, [str](int a, int b) { return str[a] < str[b]; });
  int len = 1;
  int *const cnt = lcp;
  int *coor = tmp;
  int *rank = suffix_rank;
  int rank_size = suffix_array_build_rank(N, suffix, str, rank, 0);
  while (rank_size != N && len < N) {
    int cur = 0;
    coor[cur++] = N - len;
    for (int i = 0; i < N; i++) {
      if (suffix[i] >= len) {
        coor[cur++] = suffix[i] - len;
      }
    }
    memset(cnt, 0, rank_size * sizeof(int));
    for (int i = 0; i < N; i++) {
      ++cnt[rank[i]];
    }
    for (int i = 1; i < rank_size; i++) {
      cnt[i] += cnt[i - 1];
    }
    for (int i = N - len; i >= 0; --i) {
      int cur = coor[i];
      suffix[--cnt[rank[cur]]] = cur;
    }
    int *old_rank = rank;
    rank = coor;
    coor = old_rank;
    rank_size = suffix_array_build_rank(N, suffix, old_rank, rank, len);
    len <<= 1;
  }
  if (suffix_rank != rank) {
    memcpy(suffix_rank, rank, N * sizeof(int));
  }
  len = 0;
  for (int i = 0; i < N; i++) {
    if (rank[i] == 0) {
      continue;
    }
    if (len != 0) {
      --len;
    }
    int prev = suffix[rank[i] - 1];
    while (i + len < N && prev + len < N && str[i + len] == str[prev + len]) {
      ++len;
    }
    lcp[rank[i]] = len;
  }
  lcp[0] = 0;
}
struct set_node {
  set_node *father;
  int rank;
  int left, right;
} set_nodes[(100000 << 1)];
void set_init(set_node set_nodes[], int T) {
  for (int i = 0; i < T; i++) {
    set_nodes[i].father = set_nodes + i;
    set_nodes[i].rank = 0;
    set_nodes[i].left = set_nodes[i].right = i;
  }
}
set_node *set_find(set_node *x) {
  while (x->father != x) {
    set_node *father = x->father;
    x->father = father->father;
    x = father;
  }
  return x;
}
set_node *set_union(set_node *x, set_node *y) {
  set_node *x_root = set_find(x);
  set_node *y_root = set_find(y);
  if (x_root == y_root) {
    return x_root;
  }
  set_node *father = x_root->rank > y_root->rank ? x_root : y_root;
  x_root->father = y_root->father = father;
  father->rank += x_root->rank == y_root->rank;
  father->left = min(x_root->left, y_root->left);
  father->right = max(x_root->right, y_root->right);
  return father;
}
int suffix_rank[(100000 << 1)];
int suffix[(100000 << 1)];
int lcp[(100000 << 1)];
int tmp[(100000 << 1)];
int N;
int height[100000];
int diff[(100000 << 1)];
int sum[(100000 << 1)];
int ans[100000];
struct query {
  int len;
  int start;
  int sub;
  int x, y;
  int first, second;
} queries[100000];
bool operator<(const query &a, const query &b) { return a.len > b.len; }
pair<int, int> lcp_values[(100000 << 1)];
pair<int, pair<bool, query *>> ranges[100000 << 1];
int base[100000 + 1];
void tree_array_add(int pos, int val) {
  while (pos <= 100000) {
    base[pos] += val;
    pos += ((pos) & (-pos));
  }
}
int tree_array_sum(int pos) {
  int val = 0;
  while (pos) {
    val += base[pos];
    pos -= ((pos) & (-pos));
  }
  return val;
}
int main() {
  scanf("%d", &N);
  for (int i = 0; i < N; ++i) {
    scanf("%d", height + i);
  }
  int L = N - 1;
  for (int i = 0; i < N - 1; ++i) {
    diff[i] = height[i] - height[i + 1];
  }
  for (int i = 0; i < N - 1; ++i) {
    diff[L + i] = height[i + 1] - height[i];
  }
  L <<= 1;
  build_suffix_array(L, diff, suffix, suffix_rank, lcp, tmp);
  int count = 0;
  for (int i = 0; i < L; ++i) {
    if (suffix[i] >= N - 1) {
      ++count;
    }
    sum[i] = count;
  }
  for (int i = 0; i < L; ++i) {
    lcp_values[i].first = lcp[i];
    lcp_values[i].second = i;
  }
  int Q;
  scanf("%d", &Q);
  int XQ = 0;
  for (int i = 0; i < Q; ++i) {
    int l, r;
    scanf("%d %d", &l, &r);
    if (l == r) {
      ans[i] = N - 1;
    } else {
      queries[XQ].len = r - l;
      queries[XQ].start = suffix_rank[l - 1];
      queries[XQ].x = l;
      queries[XQ].y = r;
      queries[XQ].sub = i;
      ++XQ;
    }
  }
  sort(queries, queries + XQ);
  sort(lcp_values, lcp_values + L, greater<pair<int, int>>());
  set_init(set_nodes, L);
  int lcp_cnt = 0;
  for (int i = 0; i < XQ; i++) {
    while (lcp_values[lcp_cnt].first >= queries[i].len) {
      int rank = lcp_values[lcp_cnt].second;
      set_union(set_nodes + rank - 1, set_nodes + rank);
      ++lcp_cnt;
    }
    set_node *node = set_find(queries[i].start + set_nodes);
    ranges[i << 1].first = node->left - 1;
    ranges[i << 1].second.first = true;
    ranges[i << 1].second.second = queries + i;
    ranges[(i << 1) + 1].first = node->right;
    ranges[(i << 1) + 1].second.first = false;
    ranges[(i << 1) + 1].second.second = queries + i;
    ans[queries[i].sub] =
        sum[node->right] - sum[node->left] + (suffix[node->left] >= N - 1);
  }
  int XR = XQ << 1;
  sort(ranges, ranges + XR);
  memset(base, 0, sizeof(int) * (N + 1));
  int suf_cnt = 0;
  for (int i = 0; i < XR; ++i) {
    while (suf_cnt <= ranges[i].first) {
      if (suffix[suf_cnt] >= N - 1) {
        tree_array_add(suffix[suf_cnt] - N + 2, 1);
      }
      ++suf_cnt;
    }
    const query *q = ranges[i].second.second;
    int start = max(0, q->x * 2 - q->y - 1);
    int end = q->y;
    int sum = tree_array_sum(end) - tree_array_sum(start);
    if (ranges[i].second.first) {
      ans[q->sub] += sum;
    } else {
      ans[q->sub] -= sum;
    }
  }
  for (int i = 0; i < Q; ++i) {
    printf("%d\n", ans[i]);
  }
}