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>
#pragma comment(linker, "/STACK:20000000")
using namespace std;
int ri() {
  int x;
  scanf("%d", &x);
  return x;
}
long long rll() {
  long long x;
  scanf("%lld", &x);
  return x;
}
int mas[200500];
int tmas[200500];
int d1[100500];
int d2[100500];
int cnt[200500];
int p1[200500];
int p2[200500];
int c[20][200500];
void suffArray(int n) {
  for (int i = (int)(0); i <= (int)(n - 1); i++) cnt[mas[i]]++;
  for (int i = (int)(1); i <= (int)(200499); i++) cnt[i] += cnt[i - 1];
  for (int i = (int)(0); i <= (int)(n - 1); i++) p1[--cnt[mas[i]]] = i;
  c[0][p1[0]] = 0;
  int cl = 0;
  for (int i = (int)(1); i <= (int)(n - 1); i++) {
    if (mas[p1[i]] != mas[p1[i - 1]]) cl++;
    c[0][p1[i]] = cl;
  }
  for (int mid = 1, step = 0; mid < n; mid <<= 1, step++) {
    for (int i = (int)(0); i <= (int)(cl); i++) cnt[i] = 0;
    for (int i = (int)(0); i <= (int)(n - 1); i++)
      p2[i] = (p1[i] - mid + n + n) % n;
    for (int i = (int)(0); i <= (int)(n - 1); i++) cnt[c[step][p2[i]]]++;
    for (int i = (int)(1); i <= (int)(cl); i++) cnt[i] += cnt[i - 1];
    for (int i = (int)(n - 1); i >= (int)(0); i--)
      p1[--cnt[c[step][p2[i]]]] = p2[i];
    c[step + 1][p1[0]] = 0;
    cl = 0;
    for (int i = (int)(1); i <= (int)(n - 1); i++) {
      int a = (p1[i] + mid) % n;
      int b = (p1[i - 1] + mid) % n;
      if (c[step][p1[i]] != c[step][p1[i - 1]] || c[step][a] != c[step][b])
        cl++;
      c[step + 1][p1[i]] = cl;
    }
  }
}
int getLCP(int a, int b, int mx) {
  int res = 0;
  int m = min(mx - a, mx - b);
  int L = 0;
  while ((1 << L) <= m) L++;
  L--;
  for (int i = (int)(L); i >= (int)(0); i--)
    if (c[i][a] == c[i][b]) {
      res += (1 << i);
      a += (1 << i);
      b += (1 << i);
      a %= mx;
      b %= mx;
    }
  return min(res, mx);
}
int posD1[100500];
vector<int> tree[8 * 100500];
int N;
void build(int v, int l, int r) {
  if (l == r) {
    if (p1[l] >= N) tree[v].push_back(p1[l] - N);
    return;
  }
  int mid = (l + r) >> 1;
  build(2 * v, l, mid);
  build(2 * v + 1, mid + 1, r);
  tree[v].resize(tree[2 * v].size() + tree[2 * v + 1].size());
  merge(tree[2 * v].begin(), tree[2 * v].end(), tree[2 * v + 1].begin(),
        tree[2 * v + 1].end(), tree[v].begin());
}
int get(int v, int l, int r, int L, int R, int h1, int h2) {
  if (l == L && r == R) {
    if (tree[v].empty()) return 0;
    int res = 0;
    int tl = 0, tr = tree[v].size();
    if (tree[v][0] <= h2) {
      while (tl + 1 < tr) {
        int mid = (tl + tr) >> 1;
        if (tree[v][mid] <= h2)
          tl = mid;
        else
          tr = mid;
      }
      res += tl + 1;
    }
    tl = 0, tr = tree[v].size();
    if (tree[v][0] < h1) {
      while (tl + 1 < tr) {
        int mid = (tl + tr) >> 1;
        if (tree[v][mid] < h1)
          tl = mid;
        else
          tr = mid;
      }
      res -= tl + 1;
    }
    return res;
  }
  int mid = (l + r) >> 1;
  if (R <= mid) return get(2 * v, l, mid, L, R, h1, h2);
  if (L > mid) return get(2 * v + 1, mid + 1, r, L, R, h1, h2);
  return get(2 * v, l, mid, L, mid, h1, h2) +
         get(2 * v + 1, mid + 1, r, mid + 1, R, h1, h2);
}
void solve() {
  int n = ri();
  for (int i = (int)(1); i <= (int)(n); i++) mas[i] = ri();
  for (int i = (int)(1); i <= (int)(n - 1); i++)
    d1[i] = mas[i] - mas[i + 1], d2[i] = -d1[i];
  for (int i = (int)(0); i <= (int)(n - 2); i++) mas[i] = d1[i + 1];
  mas[n - 1] = 2000000000;
  for (int i = (int)(0); i <= (int)(n - 2); i++) mas[i + n] = d2[i + 1];
  mas[2 * n - 1] = 2000000001;
  memcpy(tmas, mas, sizeof(mas));
  sort(tmas, tmas + 2 * n);
  map<int, int> index;
  int cn = 0;
  for (int i = (int)(0); i <= (int)(2 * n - 1); i++)
    if (i == 0)
      index[tmas[i]] = cn++;
    else if (tmas[i] != tmas[i - 1])
      index[tmas[i]] = cn++;
  for (int i = (int)(0); i <= (int)(2 * n - 1); i++) mas[i] = index[mas[i]];
  suffArray(n + n);
  for (int i = (int)(0); i <= (int)(2 * n - 1); i++)
    if (p1[i] <= n - 2) posD1[p1[i]] = i;
  N = n;
  build(1, 0, 2 * n - 1);
  int q = ri();
  for (int i = (int)(0); i <= (int)(q - 1); i++) {
    int L = ri() - 1, R = ri() - 1;
    int pos = posD1[L];
    int lb = pos, rb = pos;
    int l = -1, r = pos;
    while (l + 1 < r) {
      int mid = (l + r) >> 1;
      if (getLCP(p1[mid], p1[pos], 2 * n) >= R - L)
        r = mid;
      else
        l = mid;
    }
    lb = r;
    l = pos, r = 2 * n;
    while (l + 1 < r) {
      int mid = (l + r) >> 1;
      if (getLCP(p1[pos], p1[mid], 2 * n) >= R - L)
        l = mid;
      else
        r = mid;
    }
    rb = l;
    int res = 0;
    if (L == R)
      res = n - 1;
    else {
      int tl = L, tr = R;
      if (tl - tr + tl - 1 >= 0)
        res += get(1, 0, 2 * n - 1, lb, rb, 0, tl - tr + tl - 1);
      if (tr + 1 <= n - 1) res += get(1, 0, 2 * n - 1, lb, rb, tr + 1, n - 1);
    }
    printf("%d\n", res);
  }
}
int main() {
  solve();
  return 0;
}