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 = 300100;
struct SuffixArray {
  int s[maxn];
  int sa[maxn];
  int rk[maxn];
  int height[maxn];
  int t[maxn], t2[maxn], c[maxn];
  int n;
  void clear() { n = 0; }
  void build_sa(int m) {
    int i, *x = t, *y = t2;
    for (i = 0; i < m; i++) c[i] = 0;
    for (i = 0; i < n; i++) c[x[i] = s[i]]++;
    for (i = 1; i < m; i++) c[i] += c[i - 1];
    for (i = n - 1; i >= 0; i--) sa[--c[x[i]]] = i;
    for (int k = 1; k <= n; k <<= 1) {
      int p = 0;
      for (i = n - k; i < n; i++) y[p++] = i;
      for (i = 0; i < n; i++)
        if (sa[i] >= k) y[p++] = sa[i] - k;
      for (i = 0; i < m; i++) c[i] = 0;
      for (i = 0; i < n; i++) c[x[y[i]]]++;
      for (i = 0; i < m; i++) c[i] += c[i - 1];
      for (i = n - 1; i >= 0; i--) sa[--c[x[y[i]]]] = y[i];
      swap(x, y);
      p = 1;
      x[sa[0]] = 0;
      for (i = 1; i < n; i++)
        x[sa[i]] = y[sa[i - 1]] == y[sa[i]] && y[sa[i - 1] + k] == y[sa[i] + k]
                       ? p - 1
                       : p++;
      if (p >= n) break;
      m = p;
    }
  }
  void build_height() {
    int i, j, k = 0;
    for (i = 0; i < n; i++) rk[sa[i]] = i;
    for (i = 0; i < n - 1; i++) {
      if (k) k--;
      int j = sa[rk[i] - 1];
      while (s[i + k] == s[j + k]) k++;
      height[rk[i]] = k;
    }
  }
  void add(int ch) { s[n++] = ch; }
};
SuffixArray sa;
char s[maxn];
int Log[maxn];
int lcp[maxn][20];
int callcp(int a, int b) {
  int tt = b - a + 1, len = Log[tt];
  return min(lcp[a][len], lcp[a + tt - (1 << len)][len]);
}
int callo(int x, int len) {
  int l = 0, r = x;
  while (l + 1 < r) {
    int mid = (l + r) >> 1;
    if (callcp(mid + 1, x) >= len)
      r = mid;
    else
      l = mid;
  }
  return r;
}
int calup(int x, int len) {
  int l = x, r = sa.n;
  while (l + 1 < r) {
    int mid = (l + r) >> 1;
    if (callcp(x + 1, mid) >= len)
      l = mid;
    else
      r = mid;
  }
  return l;
}
int a[maxn], b[maxn];
int tree[maxn], ls[maxn * 20], rs[maxn * 20], c[maxn * 20];
int idx;
int update(int rt, int pos) {
  int nrt = idx++, ret = nrt;
  c[nrt] = c[rt] + 1;
  int l = 0, r = sa.n;
  while (l ^ r) {
    int mid = (l + r) >> 1;
    if (pos <= mid) {
      ls[nrt] = idx++;
      rs[nrt] = rs[rt];
      nrt = ls[nrt];
      rt = ls[rt];
      r = mid;
    } else {
      rs[nrt] = idx++;
      ls[nrt] = ls[rt];
      nrt = rs[nrt];
      rt = rs[rt];
      l = mid + 1;
    }
    c[nrt] = c[rt] + 1;
  }
  return ret;
}
int qmi(int rt, int pos) {
  int l = 0, r = sa.n;
  int ret = 0;
  while (l ^ r) {
    int mid = (l + r) >> 1;
    if (pos <= mid) {
      rt = ls[rt];
      r = mid;
    } else {
      ret += c[ls[rt]];
      rt = rs[rt];
      l = mid + 1;
    }
  }
  ret += c[rt];
  return ret;
}
int qma(int rt, int pos) {
  int l = 0, r = sa.n;
  int ret = 0;
  while (l ^ r) {
    int mid = (l + r) >> 1;
    if (pos <= mid) {
      ret += c[rs[rt]];
      rt = ls[rt];
      r = mid;
    } else {
      rt = rs[rt];
      l = mid + 1;
    }
  }
  ret += c[rt];
  return ret;
}
int main() {
  Log[0] = -1;
  for (int i = 1; i < maxn; i++) {
    Log[i] = Log[i >> 1] + 1;
  }
  int n;
  scanf("%d", &n);
  int bb = 0;
  for (int i = 0; i < n; i++) {
    scanf("%d", &a[i]);
    if (i) b[bb++] = (a[i] - a[i - 1]), b[bb++] = a[i - 1] - a[i];
  }
  sort(b, b + bb);
  bb = unique(b, b + bb) - b;
  for (int i = 1; i < n; i++) {
    int x = lower_bound(b, b + bb, a[i] - a[i - 1]) - b;
    x++;
    sa.add(x);
  }
  sa.add(bb + 1);
  for (int i = 1; i < n; i++) {
    int x = lower_bound(b, b + bb, a[i - 1] - a[i]) - b;
    x++;
    sa.add(x);
  }
  sa.add(0);
  sa.build_sa(bb + 2);
  sa.build_height();
  for (int i = 1; i < sa.n; i++) {
    lcp[i][0] = sa.height[i];
  }
  for (int j = 1; j < 20; j++) {
    for (int i = 1; i + (1 << j) <= sa.n; i++) {
      lcp[i][j] = min(lcp[i][j - 1], lcp[i + (1 << (j - 1))][j - 1]);
    }
  }
  idx = 1;
  for (int i = 0; i < sa.n; i++) {
    tree[i + 1] = update(tree[i], sa.sa[i]);
  }
  int q;
  scanf("%d", &q);
  idx = 1;
  while (q--) {
    int x, y;
    scanf("%d%d", &x, &y);
    x--;
    y--;
    if (x == y)
      printf("%d\n", n - 1);
    else {
      int l = callo(sa.rk[x], y - x), u = calup(sa.rk[x], y - x);
      x += n, y += n;
      int aa = qmi(tree[u + 1], max(x - (y - x) - 1, n - 1)) -
               qmi(tree[l], max(x - (y - x) - 1, n - 1));
      int bb = qmi(tree[u + 1], n - 1) - qmi(tree[l], n - 1);
      int cc = qma(tree[u + 1], y + 1) - qma(tree[l], y + 1);
      printf("%d\n", cc + (aa - bb));
    }
  }
  return 0;
}