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, m;
int a[411111], l[411111], r[411111], d[411111], ans[411111];
int c[21][411111];
pair<pair<int, int>, int> p[411111];
int u[411111];
int s[411111];
vector<pair<int, int> > v[411111];
int st[21][411111];
int lg[411111];
void gg(int it) {
  sort(p, p + n + n);
  int e = 0;
  for (int i = 0; i < n + n; i++) {
    if (i == 0 || p[i - 1].first != p[i].first) e++;
    c[it][p[i].second] = e;
  }
}
int lcp(int x, int y) {
  int r = 0;
  for (int i = 18; i >= 0; i--) {
    if (x + (1ll << (i)) > n + n) continue;
    if (y + (1ll << (i)) > n + n) continue;
    if (c[i][x] != c[i][y]) continue;
    x += (1ll << (i));
    y += (1ll << (i));
    r += (1ll << (i));
  }
  return r;
}
void add(int x) {
  x++;
  while (x <= n) {
    s[x]++;
    x = x + x - (x & (x - 1));
  }
}
int find(int x) {
  x++;
  if (x > n) x = n;
  int r = 0;
  while (x > 0) {
    r += s[x];
    x &= x - 1;
  }
  return r;
}
int fm(int x, int y) {
  if (x == y) return 1e9;
  y--;
  int w = lg[y - x + 1];
  return min(st[w][x], st[w][y - (1ll << (w)) + 1]);
}
int main() {
  cin >> n;
  lg[1] = 0;
  for (int i = 2; i <= n + n; i++) lg[i] = lg[i / 2] + 1;
  for (int i = 0; i < n; i++) scanf("%d", &a[i]);
  for (int i = n - 1; i > 0; i--) a[i] = a[i] - a[i - 1];
  for (int i = 0; i < n; i++) a[i + n] = -a[i];
  for (int i = 0; i < n + n; i++) p[i] = make_pair(make_pair(a[i], 0), i);
  gg(0);
  for (int i = 1; (1ll << (i)) <= n + n; i++) {
    for (int j = 0; j < n + n; j++)
      p[j] =
          make_pair(make_pair(c[i - 1][j], c[i - 1][j + (1ll << (i - 1))]), j);
    gg(i);
  }
  for (int i = 0; i < n + n; i++)
    if (p[i].second >= n) u[p[i].second - n] = i;
  for (int i = 0; i < n + n - 1; i++)
    st[0][i] = lcp(p[i].second, p[i + 1].second);
  for (int it = 1; (1 << it) <= n + n; it++) {
    for (int i = 0; i < n + n; i++)
      st[it][i] = min(st[it - 1][i], st[it - 1][i + (1ll << (it - 1))]);
  }
  cin >> m;
  for (int i = 0; i < m; i++) {
    scanf("%d%d", &l[i], &r[i]);
    d[i] = r[i] - l[i] + 1;
    l[i]--;
    r[i]--;
    if (l[i] == r[i]) {
      ans[i] = n - 1;
      continue;
    }
    int x = u[l[i] + 1];
    int left = x;
    int right = x;
    for (int j = 18; j >= 0; j--)
      if (right + (1ll << (j)) < n + n &&
          fm(x, right + (1ll << (j))) >= r[i] - l[i])
        right += (1ll << (j));
    for (int j = 18; j >= 0; j--)
      if (left - (1ll << (j)) >= 0 && fm(left - (1ll << (j)), x) >= r[i] - l[i])
        left -= (1ll << (j));
    v[right].push_back(make_pair(i, 1));
    if (left >= 0) v[left - 1].push_back(make_pair(i, -1));
  }
  for (int i = 0; i < n + n; i++) {
    if (p[i].second < n) add(p[i].second);
    for (int j = 0; j < v[i].size(); j++) {
      int t = find(n - 1) - find(r[v[i][j].first] + 1) +
              find(r[v[i][j].first] - d[v[i][j].first] * 2 + 2) - find(0);
      ans[v[i][j].first] += t * v[i][j].second;
    }
  }
  for (int i = 0; i < m; i++) printf("%d\n", ans[i]);
  return 0;
}