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 qMin(int x, int y) { return (x < y) ? x : y; }
bool cmpdc(pair<int, int> x, pair<int, int> y) { return x.first < y.first; }
struct _QUES {
  int l, r, datl, datr, no;
  void in(int _l, int _r, int _dl, int _dr, int _no) {
    l = _l;
    r = _r;
    datl = _dl;
    datr = _dr;
    no = _no;
  }
} Q[200005];
struct _DISCRE {
  pair<int, int> sor[200005];
  void solve(int *in, int *out, int n) {
    for (int i = 1; i <= n; i++) sor[i] = pair<int, int>(in[i], i);
    sort(sor + 1, sor + n + 1, cmpdc);
    int cnt = 1;
    out[sor[1].second] = 1;
    for (int i = 2; i <= n; i++)
      out[sor[i].second] = (sor[i].first == sor[i - 1].first) ? cnt : ++cnt;
  }
} discre;
struct _SA {
  int tax[200005];
  bool cmp(int *f, int x, int y, int w) {
    return f[x] == f[y] && f[x + w] == f[y + w];
  }
  void makesa(int *rank, int *tp, int *sa, int *hei, int *f, int n) {
    int *swp;
    memset(tax, 0, sizeof(tax));
    for (int i = 1; i <= n; i++) tax[rank[i] = f[i]]++;
    for (int i = 1; i <= n; i++) tax[i] += tax[i - 1];
    for (int i = n; i >= 1; i--) sa[tax[rank[i]]--] = i;
    for (int w = 1; w < n; w *= 2) {
      int cnt = 0;
      for (int i = n - w + 1; i <= n; i++) tp[++cnt] = i;
      for (int i = 1; i <= n; i++)
        if (sa[i] > w) tp[++cnt] = sa[i] - w;
      memset(tax, 0, sizeof(tax));
      for (int i = 1; i <= n; i++) tax[rank[i]]++;
      for (int i = 1; i <= n; i++) tax[i] += tax[i - 1];
      for (int i = n; i >= 1; i--) sa[tax[rank[tp[i]]]--] = tp[i];
      swp = rank;
      rank = tp;
      tp = swp;
      cnt = 1;
      rank[sa[1]] = 1;
      for (int i = 2; i <= n; i++)
        rank[sa[i]] = cmp(tp, sa[i - 1], sa[i], w) ? cnt : ++cnt;
      if (cnt == n) break;
    }
    int j, k = 0;
    for (int i = 1; i <= n; hei[rank[i++]] = k)
      for (j = sa[rank[i] - 1], k ? k-- : 0; f[i + k] == f[j + k]; k++)
        ;
  }
} sufa;
struct RMQ {
  int f[200005][20], n;
  void in(int *a, int _n) {
    n = _n;
    for (int i = 1; i <= n; i++) f[i][0] = a[i];
    for (int j = 1, k = 2; k <= n; j++, k *= 2) {
      for (int i = 1; i + k - 1 <= n; i++) {
        f[i][j] = qMin(f[i][j - 1], f[i + k / 2][j - 1]);
      }
    }
  }
  int out(int x, int y) {
    int sp = int(log10(y - x + 1) / log10(2) + 0.000001);
    return qMin(f[x][sp], f[y - (1 << sp) + 1][sp]);
  }
} rmq;
struct BTREE {
  int T[200005], n;
  void setsize(int _n) {
    memset(T, 0, sizeof(T));
    n = _n;
  }
  void update(int x) {
    for (; x <= n; x += x & (-x)) T[x]++;
  }
  int ask(int x) {
    int ret = 0;
    for (; x > 0; x -= x & (-x)) ret += T[x];
    return ret;
  }
} T;
bool qcmpl(_QUES x, _QUES y) { return x.datl < y.datl; }
bool qcmpr(_QUES x, _QUES y) { return x.datr > y.datr; }
int qcnt;
int sa[200005], rk[2][200005 + 200005], hei[200005], f[200005], lisan[200005];
int h[200005], lab[200005], up[200005], dow[200005], pos[200005];
int nsa[200005], nrk[200005];
int n, m, x, y, Ans[200005];
void work(int n, int iso) {
  discre.solve(f, lisan, n);
  sufa.makesa(rk[0], rk[1], sa, hei, lisan, n);
  int fir, cnt, cur = 1000000007;
  for (int i = 1; i <= n; i++)
    if (sa[i] < iso) {
      fir = i;
      break;
    }
  lab[fir] = cnt = 1;
  nsa[1] = sa[fir];
  for (int i = fir + 1; i <= n; i++) {
    cur = qMin(cur, hei[i]);
    if (sa[i] < iso) {
      h[++cnt] = cur;
      cur = 1000000007;
      lab[i] = cnt;
      nsa[cnt] = sa[i];
    }
  }
  fir = 0;
  cur = 1000000007;
  for (int i = 1; i <= n; i++) {
    cur = qMin(cur, hei[i]);
    if (sa[i] < iso) {
      fir = lab[i];
      cur = 1000000007;
    } else {
      pos[sa[i] - iso] = fir;
      up[sa[i] - iso] = cur;
    }
  }
  cur = 1000000007;
  for (int i = n; i >= 1; i--) {
    cur = qMin(cur, hei[i + 1]);
    if (sa[i] < iso)
      cur = 1000000007;
    else
      dow[sa[i] - iso] = cur;
  }
  rmq.in(h, iso - 1);
}
pair<int, int> query(int x, int len) {
  pair<int, int> ret;
  if (up[x] < len)
    ret.first = pos[x] + 1;
  else {
    int L = 1, R = pos[x];
    while (L <= R) {
      int mid = (L + R) >> 1;
      if (rmq.out(mid, pos[x]) >= len)
        R = mid - 1;
      else
        L = mid + 1;
    }
    ret.first = L - 1;
  }
  if (dow[x] < len)
    ret.second = pos[x];
  else {
    if (pos[x] + 2 > n)
      ret.second = pos[x] + 1;
    else {
      int L = pos[x] + 2, R = n;
      while (L <= R) {
        int mid = (L + R) >> 1;
        if (rmq.out(pos[x] + 2, mid) >= len)
          L = mid + 1;
        else
          R = mid - 1;
      }
      ret.second = R;
    }
  }
  return ret;
}
void sort_query() {
  scanf("%d", &m);
  qcnt = 0;
  for (int i = 1; i <= m; i++) {
    scanf("%d%d", &x, &y);
    if (x == y) {
      Ans[i] = n - 1;
      continue;
    }
    pair<int, int> tmp = query(x, y - x);
    if (tmp.first <= tmp.second) {
      int L = x + x - 1 - y, R = y + 1;
      if (L < 0) L = 0;
      if (R > n) R = n;
      Q[++qcnt].in(tmp.first, tmp.second, L, R, i);
    } else
      Ans[i] = 0;
  }
}
void do_query() {
  for (int i = 1; i <= n; i++) nrk[nsa[i]] = i;
  sort(Q + 1, Q + qcnt + 1, qcmpl);
  T.setsize(n);
  int cnt = 0, j = 1;
  while (Q[j].datl == 0) ++j;
  for (int i = 1; i < n; i++) {
    T.update(nrk[i]);
    while (j <= qcnt && Q[j].datl == i) {
      Ans[Q[j].no] += T.ask(Q[j].r) - T.ask(Q[j].l - 1);
      ++j;
    }
  }
  while (j <= qcnt) {
    Ans[Q[j].no] += T.ask(Q[j].r) - T.ask(Q[j].l - 1);
    ++j;
  }
  sort(Q + 1, Q + qcnt + 1, qcmpr);
  T.setsize(n);
  cnt = 0, j = 1;
  while (Q[j].datr >= n) ++j;
  for (int i = n - 1; i >= 1; i--) {
    T.update(nrk[i]);
    while (j <= qcnt && Q[j].datr == i) {
      Ans[Q[j].no] += T.ask(Q[j].r) - T.ask(Q[j].l - 1);
      ++j;
    }
  }
  while (j <= qcnt) {
    Ans[Q[j].no] += T.ask(Q[j].r) - T.ask(Q[j].l - 1);
    ++j;
  }
}
void print() {
  for (int i = 1; i <= m; i++) printf("%d\n", Ans[i]);
}
int main() {
  scanf("%d%d", &n, &x);
  for (int i = 2; i <= n; i++) {
    scanf("%d", &y);
    f[i - 1] = y - x;
    x = y;
  }
  f[n] = -1000000007;
  for (int i = 1; i < n; i++) f[n + i] = -f[i];
  work(n + n - 1, n);
  sort_query();
  do_query();
  print();
  return 0;
}