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 INF = 0x3fffffff;
const int SINF = 0x7fffffff;
const long long LINF = 0x3fffffffffffffff;
const long long SLINF = 0x7fffffffffffffff;
const int MAXN = 200007;
const int MAXL = 21;
int qlg[MAXN];
class saT {
 public:
  void calc() {
    calcsa();
    calclcp();
    calcrmq();
  }
  void calcsa() {
    memset(cc, 0, sizeof(cc));
    map<int, int> mcc;
    for (int i = 0; i < size; ++i) ++mcc[s[i]];
    for (map<int, int>::iterator it = mcc.begin(), pit = it++; it != mcc.end();
         ++it, ++pit)
      it->second += pit->second;
    for (int i = size - 1; i >= 0; --i) sa[mcc[s[i]]--] = i;
    int kr, kp = 1;
    rnk[sa[1]] = kp++;
    for (int i = 2; i <= size; ++i)
      rnk[sa[i]] = (s[sa[i]] == s[sa[i - 1]]) ? (kp - 1) : kp++;
    for (int j = 1; kp <= size; j <<= 1) {
      kr = 0;
      for (int i = size - j; i < size; ++i) tr[kr++] = i;
      for (int i = 1; i <= size; ++i)
        if (sa[i] >= j) tr[kr++] = sa[i] - j;
      memset(cc, 0, sizeof(cc));
      for (int i = 0; i < size; ++i) ++cc[rnk[i]];
      for (int i = 1; i < kp; ++i) cc[i] += cc[i - 1];
      for (int i = size - 1; i >= 0; --i) sa[cc[rnk[tr[i]]]--] = tr[i];
      kp = 1;
      tmp[sa[1]] = kp++;
      for (int i = 2; i <= size; ++i)
        tmp[sa[i]] = ((sa[i] + j < size) && (sa[i - 1] + j < size) &&
                      (rnk[sa[i]] == rnk[sa[i - 1]]) &&
                      (rnk[sa[i] + j] == rnk[sa[i - 1] + j]))
                         ? (kp - 1)
                         : kp++;
      memcpy(rnk, tmp, sizeof(tmp));
    }
    sa[0] = size, rnk[size] = 0;
  }
  void calclcp() {
    for (int i = 0, h = 0, j; i < size; lcp[rnk[i++] - 1] = h)
      for (j = sa[rnk[i] - 1], h ? --h : 0;
           i + h < size && j + h < size && s[i + h] == s[j + h]; ++h)
        ;
  }
  void calcrmq() {
    for (int i = 0; i < size; ++i) rmq[i][0] = lcp[i];
    int base;
    for (int j = 1; j < MAXL; ++j) {
      base = (1 << (j - 1));
      for (int i = size - base - 1; i >= 0; --i)
        rmq[i][j] = (((rmq[i][j - 1]) < (rmq[i + base][j - 1]))
                         ? (rmq[i][j - 1])
                         : (rmq[i + base][j - 1]));
    }
  }
  int rmqreq(int l, int r) {
    if (l == r) return (size - sa[r]);
    int g = qlg[r - l];
    return (((rmq[l][g]) < (rmq[r - (1 << g)][g])) ? (rmq[l][g])
                                                   : (rmq[r - (1 << g)][g]));
  }
  int size;
  int s[MAXN], rnk[MAXN], sa[MAXN], tmp[MAXN], tr[MAXN], lcp[MAXN], cc[MAXN];
  int rmq[MAXN][MAXL];
} sa;
class pftT {
 public:
  pftT() {
    for (int i = 0; i < MAXN; ++i) data[i].clear();
  }
  void upd(int v, int p, int x) {
    for (++p; p < MAXN; p += p & -p) data[p].push_back(v);
  }
  int sum(int vl, int vr, int p) {
    int ans = 0;
    for (++p; p; p ^= p & -p)
      ans +=
          (upper_bound(data[p].begin(), data[p].end(), vr) - data[p].begin()) -
          (lower_bound(data[p].begin(), data[p].end(), vl) - data[p].begin());
    return ans;
  }
  int query(int vl, int vr, int l, int r) {
    if (l > r) return 0;
    return sum(vl, vr, r) - sum(vl, vr, l - 1);
  }

 private:
  vector<int> data[MAXN];
} pft;
int n, q;
int a[MAXN];
int ql, qr;
void init();
void input();
void inputq();
void work();
int main() {
  init();
  input();
  work();
}
void init() {
  ios::sync_with_stdio(false);
  qlg[0] = qlg[1] = 0;
  for (int i = 2; i < MAXN; ++i) qlg[i] = qlg[i >> 1] + 1;
}
void input() {
  scanf("%d", &n);
  for (int i = 0; i < n; ++i) scanf("%d", &a[i]);
  scanf("%d", &q);
}
void inputq() { scanf("%d%d", &ql, &qr); }
void work() {
  for (int i = 0; i < n - 1; ++i) a[i] = a[i + 1] - a[i];
  --n;
  for (int i = 0; i < n; ++i) sa.s[i] = a[i];
  sa.s[n] = INF;
  for (int i = 0; i < n; ++i) sa.s[i + n + 1] = -a[i];
  int nn = n + n + 1;
  sa.size = nn;
  sa.calc();
  for (int i = 1; i <= nn; ++i) pft.upd(i, sa.sa[i], 1);
  int l, r, mid, nl, nr, len, ll;
  for (int _q = 0; _q < q; ++_q) {
    inputq();
    if (ql == qr) {
      printf("%d\n", n);
      continue;
    }
    --ql, --qr;
    --qr;
    len = qr - ql + 1;
    ll = sa.rnk[ql + n + 1];
    l = 1, r = ll;
    while (l < r) {
      mid = (l + r) >> 1;
      if (sa.rmqreq(mid, ll) >= len)
        r = mid;
      else
        l = mid + 1;
    }
    nl = l;
    l = ll, r = nn - 1;
    while (l < r) {
      mid = (l + r + 1) >> 1;
      if (sa.rmqreq(ll, mid) >= len)
        l = mid;
      else
        r = mid - 1;
    }
    nr = l;
    printf("%d\n", pft.query(nl, nr, 0, ql - len - 1) +
                       pft.query(nl, nr, qr + 2, n - 1));
  }
}