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 = 200011, logN = 20, maxint = 0x7f7f7f7f;
int n, q, h[maxn], b[maxn];
struct sa_t {
  int sa[maxn], rank[maxn], height[maxn], minV[maxn][logN], wa[maxn], wb[maxn],
      wv[maxn], ws[maxn];
  int cmp(int *r, int a, int b, int l, int n) {
    return r[a] == r[b] && (a + l > n || b + l > n || r[a + l] == r[b + l]);
  }
  void DA(int *r, int *sa, int n, int m) {
    int i, j, k, p, *x = wa, *y = wb, *t;
    for (i = 0; i < m; ++i) ws[i] = 0;
    for (i = 0; i < n; ++i) ws[x[i] = r[i]]++;
    for (i = 1; i < m; ++i) ws[i] += ws[i - 1];
    for (i = n - 1; i >= 0; --i) sa[--ws[x[i]]] = i;
    for (j = 1, p = 1; p < n; j *= 2, m = p) {
      for (p = 0, i = n - j; i < n; ++i) y[p++] = i;
      for (i = 0; i < n; ++i)
        if (sa[i] >= j) y[p++] = sa[i] - j;
      for (i = 0; i < n; ++i) wv[i] = x[y[i]];
      for (i = 0; i < m; ++i) ws[i] = 0;
      for (i = 0; i < n; ++i) ws[wv[i]]++;
      for (i = 1; i < m; ++i) ws[i] += ws[i - 1];
      for (i = n - 1; i >= 0; --i) sa[--ws[wv[i]]] = y[i];
      for (t = x, x = y, y = t, p = 1, x[sa[0]] = 0, i = 1; i < n; ++i) {
        x[sa[i]] = cmp(y, sa[i - 1], sa[i], j, n) ? p - 1 : p++;
      }
    }
    for (int i = 1; i <= n; ++i) rank[++sa[i - 1]] = i;
    k = 0;
    for (int i = 1; i <= n; ++i) {
      if (--k < 0) k = 0;
      for (int j = sa[rank[i] - 2];
           rank[i] != 1 && r[i + k - 1] == r[j + k - 1]; ++k)
        ;
      height[rank[i]] = k;
    }
  }
  void RMQ(int n) {
    for (int i = 1; i <= n; ++i) {
      minV[i][0] = height[i];
    }
    for (int k = 1; k < logN; ++k) {
      for (int i = 1; i + (1 << (k - 1)) <= n; ++i) {
        minV[i][k] = min(minV[i][k - 1], minV[i + (1 << (k - 1))][k - 1]);
      }
    }
  }
  int askRMQ(int l, int r) {
    int result = maxint, length = r - l + 1;
    for (int k = logN - 1; k >= 0; --k) {
      if (length & (1 << k)) {
        result = min(result, minV[l][k]);
        l += (length & (1 << k));
      }
    }
    return result;
  }
  int LCP(int i, int j) {
    i = rank[i], j = rank[j];
    if (i > j) swap(i, j);
    ++i;
    return askRMQ(i, j);
  }
  void init(int *s, int n, int lim) {
    memset(sa, 0, sizeof(sa)), memset(rank, 0, sizeof(rank)),
        memset(height, 0, sizeof(height)), memset(minV, 0, sizeof(minV)),
        memset(wa, 0, sizeof(wa)), memset(wb, 0, sizeof(wb)),
        memset(wv, 0, sizeof(wv)), memset(ws, 0, sizeof(ws));
    DA(s + 1, sa + 1, n, lim + 1);
    RMQ(n);
  }
} SA;
struct query_t {
  int l, r, ll, rr, ind, ans;
} Q[maxn];
struct bittree_t {
  int T[maxn];
  int lowbit(int x) { return x & (-x); }
  int query(int x) {
    if (x < 0) return 0;
    int result = 0;
    for (; x; x -= lowbit(x)) result += T[x];
    return result;
  }
  void update(int x, int delta) {
    for (; x < maxn; x += lowbit(x)) T[x] += delta;
  }
  void clear() { memset(T, 0, sizeof(T)); }
} BIT;
bool cmp1(const query_t &a, const query_t &b) {
  return a.l - (a.r - a.l) < b.l - (b.r - b.l);
}
bool cmp2(const query_t &a, const query_t &b) { return a.r > b.r; }
bool cmp3(const query_t &a, const query_t &b) { return a.ind < b.ind; }
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; ++i) {
    scanf("%d", &h[i]);
  }
  for (int i = n; i >= 2; --i) h[i] = h[i] - h[i - 1];
  h[1] = -maxint + 2;
  h[n + 1] = -maxint + 1;
  for (int i = 1; i <= n; ++i) h[i + n + 1] = -h[i];
  h[n + n + 2] = -maxint;
  for (int i = 1; i <= n + n + 2; ++i) b[i] = h[i];
  int sz = n + n + 2;
  sort(b + 1, b + 1 + sz);
  sz = unique(b + 1, b + 1 + sz) - b - 1;
  for (int i = 1; i <= n + n + 2; ++i) {
    h[i] = lower_bound(b + 1, b + sz, h[i]) - b;
  }
  SA.init(h, n + n + 2, sz);
  scanf("%d", &q);
  for (int i = 1; i <= q; ++i) {
    scanf("%d%d", &Q[i].l, &Q[i].r);
    Q[i].ind = i;
    int r = SA.rank[Q[i].l + 1], len = Q[i].r - Q[i].l, begin, end, mid;
    begin = 1, end = r;
    while (begin < end) {
      mid = (begin + end) / 2;
      if (SA.LCP(Q[i].l + 1, SA.sa[mid]) >= len) {
        end = mid;
      } else {
        begin = mid + 1;
      }
    }
    Q[i].ll = begin;
    begin = r, end = n + n + 2;
    while (begin < end) {
      mid = (begin + end) / 2 + 1;
      if (SA.LCP(Q[i].l + 1, SA.sa[mid]) >= len) {
        begin = mid;
      } else {
        end = mid - 1;
      }
    }
    Q[i].rr = begin;
  }
  int pt = 1;
  sort(Q + 1, Q + 1 + q, cmp1);
  for (int i = 1; i <= q; ++i) {
    int lim = Q[i].l - (Q[i].r - Q[i].l);
    while (pt <= lim && pt <= n) BIT.update(SA.rank[n + 1 + (pt++)], 1);
    Q[i].ans += BIT.query(Q[i].rr) - BIT.query(Q[i].ll - 1);
  }
  pt = n;
  sort(Q + 1, Q + 1 + q, cmp2);
  BIT.clear();
  for (int i = 1; i <= q; ++i) {
    int lim = Q[i].r + 2;
    while (pt >= lim && pt >= 1) BIT.update(SA.rank[n + 1 + (pt--)], 1);
    Q[i].ans += BIT.query(Q[i].rr) - BIT.query(Q[i].ll - 1);
  }
  sort(Q + 1, Q + 1 + q, cmp3);
  for (int i = 1; i <= q; ++i) {
    printf("%d\n", Q[i].l == Q[i].r ? n - 1 : Q[i].ans);
  }
  return 0;
}