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, const U &b) {
  if (a > b) a = b;
}
template <typename T, typename U>
inline void smax(T &a, const U &b) {
  if (a < b) a = b;
}
char gchar() {
  static char tbuf1[1000], *tmp_st = tbuf1, *tmp_ed = tbuf1;
  if (tmp_st == tmp_ed) {
    int len = fread(tbuf1, 1, 1000, stdin);
    if (len == 0) return -1;
    tmp_ed = tbuf1 + len;
    tmp_st = tbuf1;
  }
  return *(tmp_st++);
}
inline bool gn(int &first) {
  char c, sg = 0;
  while (c = gchar(), (c > '9' || c < '0') && c != '-' && ~c)
    ;
  if (c == -1) return 0;
  for ((c == '-' ? sg = 1, c = gchar() : 0), first = 0; c >= '0' && c <= '9';
       c = gchar())
    first = (first << 1) + (first << 3) + c - '0';
  if (sg) first = -first;
  return 1;
}
inline void print(int first) {
  if (first < 0) {
    putchar('-');
    return print(-first);
  }
  if (first < 10) {
    putchar('0' + first);
    return;
  }
  print(first / 10);
  putchar(first % 10 + '0');
}
template <class T>
inline void println(T first) {
  print(first);
  putchar('\n');
}
int s[200200];
int H[200200], Rank[200200], sa[200200];
pair<pair<int, int>, int> pos[200200];
void buildSA(int n) {
  for (int i = 1; i <= n; i++) Rank[i] = s[i];
  Rank[n + 1] = -0x3f3f3f3f - 1;
  for (int j = 0; j < 18; j++) {
    for (int i = 1; i <= n; i++) {
      pos[i] = pair<pair<int, int>, int>(
          pair<int, int>(Rank[i], Rank[min(n + 1, i + (1 << j))]), i);
    }
    sort(pos + 1, pos + n + 1);
    int cnt = 0;
    for (int i = 1; i <= n; i++) {
      Rank[pos[i].second] = cnt + 1;
      if (pos[i].first != pos[i + 1].first) cnt++;
    }
    if (cnt == n) break;
  }
  for (int i = 1; i <= n; i++) sa[Rank[i]] = i;
}
void Height(int n) {
  int h = 0;
  for (int i = 1; i <= n; i++) {
    if (Rank[i] == 1) {
      H[Rank[i]] = 0;
      continue;
    }
    h = max(0, h - 1);
    for (int j = sa[Rank[i] - 1]; s[i + h] == s[j + h]; h++)
      ;
    H[Rank[i]] = h;
  }
}
int mn[200200][18];
void rmq(int n) {
  for (int i = 1; i <= n; i++) mn[i][0] = H[i];
  for (int j = 1; j < 18; j++) {
    for (int i = 1; i <= n; i++) {
      mn[i][j] = min(mn[i][j - 1], mn[min(n, i + (1 << j - 1))][j - 1]);
    }
  }
}
int calc(int L, int R) {
  int k = 31 - __builtin_clz(R - L + 1);
  return min(mn[L][k], mn[R - (1 << k) + 1][k]);
}
int h[200200], n, N;
vector<int> B[200200];
inline void insert(int u, int first) {
  for (; u <= N; u += u & -u) B[u].push_back(first);
}
inline int calc(int u, int L, int R) {
  int ans = 0;
  L++, R--;
  for (; u; u -= u & -u) {
    ans += B[u].size();
    ans -= upper_bound(B[u].begin(), B[u].end(), R) -
           lower_bound(B[u].begin(), B[u].end(), L);
  }
  return ans;
}
int main() {
  gn(n);
  for (int i = 1; i <= n; i++) gn(h[i]);
  for (int i = 1; i < n; i++) s[i] = h[i + 1] - h[i];
  s[n] = -0x3f3f3f3f;
  for (int i = 1; i < n; i++) s[n + i] = h[i] - h[i + 1];
  N = (n << 1) - 1;
  buildSA(N);
  Height(N);
  rmq(N);
  for (int i = 1; i < n; i++) insert(Rank[i], i);
  int q;
  gn(q);
  while (q--) {
    int l, r;
    gn(l), gn(r);
    if (l == r)
      println(n - 1);
    else {
      int cc = Rank[n + l];
      int len = r - l;
      int st = 0, ed = cc;
      while (st + 1 < ed) {
        int md = st + ed >> 1;
        if (calc(md + 1, cc) < len)
          st = md;
        else
          ed = md;
      }
      int L = ed;
      st = cc, ed = N + 1;
      while (st + 1 < ed) {
        int md = st + ed >> 1;
        if (calc(cc + 1, md) < len)
          ed = md;
        else
          st = md;
      }
      int R = st;
      int ll = l - len - 1, rr = r + 1;
      println(calc(R, ll, rr) - calc(L - 1, ll, rr));
    }
  }
}