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;
template <typename T, typename U>
inline void smin(T &a, U b) {
  if (a > b) a = b;
}
template <typename T, typename U>
inline void smax(T &a, U b) {
  if (a < b) a = b;
}
inline void gn(long long &first) {
  int sg = 1;
  char c;
  while (((c = getchar()) < '0' || c > '9') && c != '-')
    ;
  c == '-' ? (sg = -1, first = 0) : (first = c - '0');
  while ((c = getchar()) >= '0' && c <= '9') first = first * 10 + c - '0';
  first *= sg;
}
inline void gn(int &first) {
  long long t;
  gn(t);
  first = t;
}
inline void gn(unsigned long long &first) {
  long long t;
  gn(t);
  first = t;
}
inline void gn(double &first) {
  double t;
  scanf("%lf", &t);
  first = t;
}
inline void gn(long double &first) {
  double t;
  scanf("%lf", &t);
  first = t;
}
int power(int a, int b, int m, int ans = 1) {
  for (; b; b >>= 1, a = 1LL * a * a % m)
    if (b & 1) ans = 1LL * ans * a % m;
  return ans;
}
int s[(100010 << 1)], a[100010], n, N, q, ans[100010];
struct suffix_array {
  pair<pair<int, int>, int> pos[(100010 << 1)];
  int Rank[(100010 << 1)], sa[(100010 << 1)], lcpm[(100010 << 1)][20];
  void build() {
    for (int i = 0; i < N; i++) Rank[i] = s[i];
    for (int j = 0; j < 20; j++) {
      for (int i = 0; i < N; i++) {
        pos[i] = pair<pair<int, int>, int>(
            pair<int, int>(Rank[i], Rank[min(N, i + (1 << j))]), i);
      }
      sort(pos, pos + N);
      int cnt = 0;
      for (int i = 0; i < N; i++) {
        Rank[pos[i].second] = cnt + 1;
        cnt += (pos[i].first != pos[i + 1].first);
      }
      if (cnt == N) break;
    }
    for (int i = 0; i < N; i++) sa[Rank[i]] = i;
    return;
  }
  void lcp_length() {
    int h = 0;
    for (int i = 0; i < N; i++) {
      if (Rank[i] == 1) {
        lcpm[Rank[i]][0] = 0;
        continue;
      }
      h = max(h - 1, 0);
      for (int j = sa[Rank[i] - 1]; s[j + h] == s[i + h]; h++)
        ;
      lcpm[Rank[i]][0] = h;
    }
    for (int j = 1; j < 20; j++) {
      for (int i = 1; i <= N; i++) {
        lcpm[i][j] = min(lcpm[i][j - 1], lcpm[min(N, i + (1 << j - 1))][j - 1]);
      }
    }
    return;
  }
  int query_(int i, int j) {
    if (i == j) return 0x3f3f3f3f;
    int k = 31 - (__builtin_clz(j - i));
    return min(lcpm[i + 1][k], lcpm[j - (1 << k) + 1][k]);
  }
} SA;
struct Query {
  int l, r, mn, mx, id;
} Q[100010];
bool cmp1(Query q1, Query q2) { return q1.l < q2.l; }
bool cmp2(Query q1, Query q2) { return q1.r < q2.r; }
struct Fenwic_tree {
  int B1[100010], B2[100010];
  void clear() {
    memset(B1, 0, sizeof(B1));
    memset(B2, 0, sizeof(B2));
    return;
  }
  void update_up(int u) {
    if (u > n) return;
    while (u > 0) {
      B1[u]++;
      u -= u & (-u);
    }
    return;
  }
  void update_down(int u) {
    if (u <= 0) return;
    while (u <= n) {
      B2[u]++;
      u += u & (-u);
    }
    return;
  }
  int calc_up(int u) {
    if (u > n) return 0;
    int ans = 0;
    while (u <= n) {
      ans += B1[u];
      u += u & (-u);
    }
    return ans;
  }
  int calc_down(int u) {
    if (u <= 0) return 0;
    int ans = 0;
    while (u > 0) {
      ans += B2[u];
      u -= u & (-u);
    }
    return ans;
  }
} BIT;
int main() {
  int l, r, id, mn, mx;
  gn(n);
  N = 2 * n;
  for (int i = 0; i < n; i++) gn(a[i]);
  for (int i = 0; i < n; i++)
    s[i] = a[i] - a[i + 1], s[n + i] = a[i + 1] - a[i];
  s[N] = 0x3f3f3f3f;
  SA.build();
  SA.lcp_length();
  gn(q);
  for (int i = 0; i < q; i++) {
    gn(l);
    gn(r);
    l--;
    r--;
    int len = r - l, p = SA.Rank[n + l];
    Q[i].id = i;
    Q[i].mn = l - len, Q[i].mx = r + 2;
    int st = 0, ed = p;
    while (ed - st > 1) {
      int mid = st + ed >> 1;
      if (SA.query_(mid, p) >= len)
        ed = mid;
      else
        st = mid;
    }
    Q[i].l = ed;
    st = p, ed = N + 1;
    while (ed - st > 1) {
      int mid = st + ed >> 1;
      if (SA.query_(p, mid) >= len)
        st = mid;
      else
        ed = mid;
    }
    Q[i].r = st;
  }
  sort(Q, Q + q, cmp2);
  int pos = 1;
  for (int i = 1; i <= N; i++) SA.sa[i]++;
  for (int i = 0; i < q; i++) {
    l = Q[i].l, r = Q[i].r, id = Q[i].id, mn = Q[i].mn, mx = Q[i].mx;
    for (int j = pos; j <= r; j++) {
      BIT.update_down(SA.sa[j]);
      BIT.update_up(SA.sa[j]);
    }
    pos = r + 1;
    ans[id] = BIT.calc_down(mn) + BIT.calc_up(mx);
  }
  BIT.clear();
  sort(Q, Q + q, cmp1);
  pos = 1;
  for (int i = 0; i < q; i++) {
    l = Q[i].l, r = Q[i].r, id = Q[i].id, mn = Q[i].mn, mx = Q[i].mx;
    for (int j = pos; j < l; j++) {
      BIT.update_down(SA.sa[j]);
      BIT.update_up(SA.sa[j]);
    }
    pos = l;
    ans[id] -= (BIT.calc_down(mn) + BIT.calc_up(mx));
  }
  for (int i = 0; i < q; i++) printf("%d\n", ans[i]);
  return 0;
}