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 Imx = 2147483647;
const long long Lbig = 2e18;
const int mod = 1e9 + 7;
struct fastio {
  char s[100000];
  int it, len;
  fastio() { it = len = 0; }
  inline char get() {
    if (it < len) return s[it++];
    it = 0;
    len = fread(s, 1, 100000, stdin);
    if (len == 0)
      return EOF;
    else
      return s[it++];
  }
  bool notend() {
    char c = get();
    while (c == ' ' || c == '\n') c = get();
    if (it > 0) it--;
    return c != EOF;
  }
} _buff;
inline long long getnum() {
  long long r = 0;
  bool ng = 0;
  char c;
  c = _buff.get();
  while (c != '-' && (c < '0' || c > '9')) c = _buff.get();
  if (c == '-') ng = 1, c = _buff.get();
  while (c >= '0' && c <= '9') r = r * 10 + c - '0', c = _buff.get();
  return ng ? -r : r;
}
template <class T>
inline void putnum(T x) {
  if (x < 0) putchar('-'), x = -x;
  register short a[20] = {}, sz = 0;
  while (x) a[sz++] = x % 10, x /= 10;
  if (sz == 0) putchar('0');
  for (int i = sz - 1; i >= 0; i--) putchar('0' + a[i]);
}
inline char getreal() {
  char c = _buff.get();
  while (c <= 32) c = _buff.get();
  return c;
}
int s[200111], sn;
int sa[200111], lcp[200111], rk[200111], nrk[200111], nsa[200111], b[200111];
int tab[20][200111];
void makeb(int n) {
  memset(b, 0, sizeof(b));
  for (int i = 1; i <= n; i++) b[rk[i]]++;
  for (int i = 1; i <= 200002; i++) b[i] += b[i - 1];
}
void construct_sa(int n) {
  sn = n;
  for (int i = 1; i <= n; i++) rk[i] = s[i];
  makeb(n);
  for (int i = n; i >= 1; i--) sa[b[rk[i]]--] = i;
  for (int k = 1; k < n; k <<= 1) {
    makeb(n);
    for (int i = n; i >= 1; i--)
      if (sa[i] > k) nsa[b[rk[sa[i] - k]]--] = sa[i] - k;
    for (int i = n; i >= 1; i--)
      if (sa[i] > n - k) nsa[b[rk[sa[i]]]--] = sa[i];
    nrk[nsa[1]] = 1;
    for (int i = 2; i <= n; i++)
      nrk[nsa[i]] =
          nrk[nsa[i - 1]] +
          (make_pair(rk[nsa[i - 1]],
                     nsa[i - 1] + k <= n ? rk[nsa[i - 1] + k] : -1) <
           make_pair(rk[nsa[i]], nsa[i] + k <= n ? rk[nsa[i] + k] : -1));
    swap(nrk, rk);
    swap(nsa, sa);
  }
  for (int i = 1; i <= n; i++) rk[sa[i]] = i;
  int h = 0;
  for (int i = 1; i <= n; i++) {
    if (h > 0) h--;
    if (rk[i] == 1) continue;
    int j = sa[rk[i] - 1];
    while (i + h <= n && j + h <= n && s[i + h] == s[j + h]) h++;
    lcp[rk[i] - 1] = h;
  }
  for (int i = 1; i < n; i++) tab[0][i] = lcp[i];
  for (int i = 1; i < 20; i++) {
    for (int j = 1; j <= n - (1 << i) + 1; j++) {
      tab[i][j] = min(tab[i - 1][j], tab[i - 1][j + (1 << (i - 1))]);
    }
  }
}
map<int, int> mp;
int mptot;
int n, q, a[100111];
void prework() {
  for (int i = 1; i < n; i++) s[i] = a[i + 1] - a[i];
  for (int i = n; i < 2 * n - 1; i++) s[i] = -s[i - n + 1];
  for (int i = 1; i < 2 * n - 1; i++) {
    int &v = mp[s[i]];
    if (v == 0) v = ++mptot;
    s[i] = v;
  }
  construct_sa(2 * n - 2);
}
int qx[100111], qy[100111], ans[100111];
pair<int, int> querylcp(int x, int v) {
  int l = x;
  for (int i = 19; i >= 0; i--) {
    if (l >= (1 << i) && tab[i][l - (1 << i)] >= v) {
      l -= 1 << i;
    }
  }
  int r = x;
  for (int i = 19; i >= 0; i--) {
    if (r + (1 << i) <= sn && tab[i][r] >= v) {
      r += 1 << i;
    }
  }
  return make_pair(l, r);
}
struct query {
  int x, y, v, id;
  query(int X = 0, int Y = 0, int V = 0, int Id = 0) {
    x = X;
    y = Y;
    v = V;
    id = Id;
  }
  bool operator<(const query &t) const { return x < t.x; }
} qr[800111];
struct BIT {
  int a[200111];
  BIT() { memset(a, 0, sizeof(a)); }
  void add(int x) {
    for (int i = x; i <= 200005; i += i & (-i)) a[i]++;
  }
  int query(int x) {
    int ret = 0;
    for (int i = x; i; i -= i & (-i)) ret += a[i];
    return ret;
  }
} bit;
int qn;
void addquery(int xl, int xr, int yl, int yr, int id) {
  if (xl > 1) qr[qn++] = query(xl - 1, yl - 1, 1, id);
  qr[qn++] = query(xr, yl - 1, -1, id);
  if (xl > 1) qr[qn++] = query(xl - 1, yr, -1, id);
  qr[qn++] = query(xr, yr, 1, id);
}
void solve() {
  sort(qr, qr + qn);
  int it = 0;
  for (int i = 1; i <= sn; i++) {
    bit.add(sa[i]);
    while (it < qn && qr[it].x <= i) {
      ans[qr[it].id] += bit.query(qr[it].y) * qr[it].v;
      it++;
    }
  }
}
int main() {
  n = getnum();
  for (int i = 1; i <= n; i++) a[i] = getnum();
  prework();
  q = getnum();
  for (int i = 1; i <= q; i++) {
    int x, y;
    x = getnum(), y = getnum();
    qx[i] = x;
    qy[i] = y;
    if (x == y)
      ans[i] = n - 1;
    else {
      pair<int, int> pp = querylcp(rk[x], y - x);
      int r = x - (y - x + 1);
      int l = y + 1;
      if (1 <= r) {
        addquery(pp.first, pp.second, 1 + n - 1, r + n - 1, i);
      }
      if (l <= n) {
        addquery(pp.first, pp.second, l + n - 1, sn, i);
      }
    }
  }
  solve();
  for (int i = 1; i <= q; i++) putnum(ans[i]), putchar('\n');
  return 0;
}