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 long long INF = 1 << 28;
const long long LINF = 1ll << 61;
int n, sz, q;
pair<int, int> tp[200111];
int a[200111], sa[200111], nsa[200111], rk[200111], nrk[200111], b[200111];
int lcp[200111], tab[20][200111];
void makeb() {
  memset(b, 0, sizeof(b));
  for (int i = 1; i <= sz; i++) b[rk[i]]++;
  for (int i = 1; i <= sz; i++) b[i] += b[i - 1];
}
void construct_sa() {
  for (int i = 1; i <= sz; i++) tp[i] = make_pair(a[i], i);
  sort(tp + 1, tp + sz + 1);
  for (int i = 1; i <= sz; i++) sa[i] = tp[i].second;
  rk[sa[1]] = 0;
  for (int i = 2; i <= sz; i++)
    rk[sa[i]] = rk[sa[i - 1]] + (a[sa[i - 1]] < a[sa[i]]);
  for (int k = 1; k < sz; k <<= 1) {
    makeb();
    for (int i = sz; i > 0; i--)
      if (sa[i] > k) nsa[b[rk[sa[i] - k]]--] = sa[i] - k;
    for (int i = sz; i > 0; i--)
      if (sa[i] > sz - k) nsa[b[rk[sa[i]]]--] = sa[i];
    nrk[nsa[1]] = 1;
    for (int i = 2; i <= sz; i++)
      nrk[nsa[i]] =
          nrk[nsa[i - 1]] +
          (make_pair(rk[nsa[i - 1]],
                     nsa[i - 1] + k > sz ? -1 : rk[nsa[i - 1] + k]) <
           make_pair(rk[nsa[i]], nsa[i] + k > sz ? -1 : rk[nsa[i] + k]));
    swap(rk, nrk);
    swap(sa, nsa);
  }
  for (int i = 1; i <= sz; i++) rk[sa[i]] = i;
  int h = 0;
  for (int i = 1; i <= sz; i++) {
    if (h > 0) h--;
    if (rk[i] == 1) continue;
    int j = sa[rk[i] - 1];
    while (i + h <= sz && j + h <= sz && a[i + h] == a[j + h]) h++;
    lcp[rk[i] - 1] = h;
  }
  for (int i = 1; i < sz; i++) tab[0][i] = lcp[i];
  for (int i = 1; i < 20; i++) {
    for (int j = 1; j <= sz - (1 << i); j++) {
      tab[i][j] = min(tab[i - 1][j], tab[i - 1][j + (1 << (i - 1))]);
    }
  }
}
int bisearchl(int x, int len) {
  int pos = x;
  for (int i = 19; i >= 0; i--) {
    if ((pos - (1 << i)) > 0 && tab[i][pos - (1 << i)] >= len) {
      pos -= (1 << i);
    }
  }
  return pos;
}
int bisearchr(int x, int len) {
  int pos = x;
  for (int i = 19; i >= 0; i--) {
    if (tab[i][pos] >= len) {
      pos += (1 << i);
    }
  }
  return pos;
}
vector<pair<pair<int, int>, int> > cmd[200111];
int ans[100111];
void query(int x, int y, int id) {
  int l = bisearchl(rk[x + n + 1], y - x + 1);
  int r = bisearchr(rk[x + n + 1], y - x + 1);
  pair<int, int> p = make_pair(max(0, x - (y - x + 1) - 1), y + 1);
  cmd[r].push_back(make_pair(p, id));
  cmd[l - 1].push_back(make_pair(p, -id));
}
struct BIT {
  int a[200111];
  BIT() { memset(a, 0, sizeof(a)); }
  void add(int x) {
    for (; x < 200111; x += x & (-x)) a[x]++;
  }
  int sum(int x) {
    int ans = 0;
    for (; x > 0; x -= x & (-x)) ans += a[x];
    return ans;
  }
} kotori;
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
  n--;
  sz = n * 2 + 1;
  for (int i = 1; i <= n; i++) a[i] -= a[i + 1];
  a[n + 1] = 2147483647;
  for (int i = 1; i <= n; i++) a[i + n + 1] = -a[i];
  construct_sa();
  scanf("%d", &q);
  for (int i = 1; i <= q; i++) {
    int x, y;
    scanf("%d%d", &x, &y);
    if (x == y)
      ans[i] = n;
    else
      query(x, y - 1, i);
  }
  int tot = 0;
  for (int i = 1; i <= sz; i++) {
    if (sa[i] <= n) kotori.add(sa[i]), tot++;
    for (int j = 0; j < cmd[i].size(); j++) {
      int x = cmd[i][j].second;
      int coef = 1;
      if (x < 0) coef = -1, x = -x;
      pair<int, int> p = cmd[i][j].first;
      int val = tot - kotori.sum(p.second) + kotori.sum(p.first);
      ans[x] += coef * val;
    }
  }
  for (int i = 1; i <= q; i++) printf("%d\n", ans[i]);
  return 0;
}