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 iinf = 1e9 + 7;
const long long linf = 1ll << 60;
const double dinf = 1e60;
template <typename T>
inline void scf(T &x) {
  bool f = 0;
  x = 0;
  char c = getchar();
  while ((c < '0' || c > '9') && c != '-') c = getchar();
  if (c == '-') {
    f = 1;
    c = getchar();
  }
  while (c >= '0' && c <= '9') {
    x = x * 10 + c - '0';
    c = getchar();
  }
  if (f) x = -x;
  return;
}
template <typename T1, typename T2>
void scf(T1 &x, T2 &y) {
  scf(x);
  return scf(y);
}
template <typename T1, typename T2, typename T3>
void scf(T1 &x, T2 &y, T3 &z) {
  scf(x);
  scf(y);
  return scf(z);
}
template <typename T1, typename T2, typename T3, typename T4>
void scf(T1 &x, T2 &y, T3 &z, T4 &w) {
  scf(x);
  scf(y);
  scf(z);
  return scf(w);
}
inline char mygetchar() {
  char c = getchar();
  while (c == ' ' || c == '\n') c = getchar();
  return c;
}
template <typename T>
void chkmax(T &x, const T &y) {
  if (y > x) x = y;
  return;
}
template <typename T>
void chkmin(T &x, const T &y) {
  if (y < x) x = y;
  return;
}
const int N = 2e5 + 100;
const int lgN = 19;
namespace ST {
int n;
int st[N][lgN], LOG[N], POW[lgN];
void init(int _n, int *a) {
  for (int i = 0; i < (lgN); ++i) POW[i] = 1 << i;
  n = _n;
  for (int i = (1); i <= (n); ++i) {
    st[i][0] = a[i];
    while (POW[LOG[i]] < i) LOG[i]++;
    LOG[i]--;
  }
  for (int j = 0; j < (lgN - 1); ++j)
    for (int i = (1); i <= (n - POW[j + 1] + 1); ++i)
      st[i][j + 1] = min(st[i][j], st[i + POW[j]][j]);
  return;
}
int Q(int l, int r) {
  if (l == r) return st[l][0];
  int lg = LOG[r - l + 1];
  return min(st[l][lg], st[r - POW[lg] + 1][lg]);
}
}  // namespace ST
namespace SA {
int n;
int sa[N], rnk[N], h[N];
void init(int _n, int *a) {
  n = _n;
  vector<int> all;
  all.clear();
  for (int i = (1); i <= (n); ++i) all.push_back(a[i]);
  sort(all.begin(), all.end());
  all.resize(unique(all.begin(), all.end()) - all.begin());
  for (int i = (1); i <= (n); ++i)
    rnk[i] = (lower_bound(all.begin(), all.end(), a[i]) - all.begin()) + 1;
  static int cnt[N];
  static pair<int, pair<int, int> > p[N], q[N];
  for (int len = 1, mx = (int)all.size(); mx < n; len <<= 1) {
    for (int i = (1); i <= (n); ++i)
      p[i] = {i, {rnk[i], (i + len <= n) ? rnk[i + len] : 0}};
    for (int i = (0); i <= (mx + 1); ++i) cnt[i] = 0;
    for (int i = (1); i <= (n); ++i) cnt[p[i].second.second + 1]++;
    for (int i = (1); i <= (mx + 1); ++i) cnt[i] += cnt[i - 1];
    for (int i = (1); i <= (n); ++i)
      cnt[p[i].second.second]++, q[cnt[p[i].second.second]] = p[i];
    for (int i = (0); i <= (mx + 1); ++i) cnt[i] = 0;
    for (int i = (1); i <= (n); ++i) cnt[q[i].second.first + 1]++;
    for (int i = (1); i <= (mx + 1); ++i) cnt[i] += cnt[i - 1];
    for (int i = (1); i <= (n); ++i)
      cnt[q[i].second.first]++, p[cnt[q[i].second.first]] = q[i];
    mx = 0;
    for (int i = (1); i <= (n); ++i) {
      if (i == 1 || p[i].second != p[i - 1].second) ++mx;
      rnk[p[i].first] = mx;
    }
  }
  rnk[n + 1] = 0;
  for (int i = (1); i <= (n + 1); ++i) sa[rnk[i]] = i;
  int height = 0;
  for (int i = (1); i <= (n); ++i) {
    int j = sa[rnk[i] - 1];
    if (height) --height;
    while (i + height <= n && j + height <= n && a[i + height] == a[j + height])
      ++height;
    h[rnk[i]] = height;
  }
  ST::init(n, h);
  return;
}
int LB(int i, int len) {
  i = rnk[i];
  int lb = 1, rb = i;
  while (lb <= rb) {
    int mid = lb + rb >> 1;
    if (ST::Q(mid, i) < len)
      lb = mid + 1;
    else
      rb = mid - 1;
  }
  return lb - 1;
}
int RB(int i, int len) {
  i = rnk[i];
  int lb = i + 1, rb = n;
  while (lb <= rb) {
    int mid = lb + rb >> 1;
    if (ST::Q(i + 1, mid) < len)
      rb = mid - 1;
    else
      lb = mid + 1;
  }
  return rb;
}
}  // namespace SA
namespace PSEG {
const int N = ::N;
struct node {
  node *l, *r;
  int num;
  node() {
    l = r = 0;
    num = 0;
  }
  void pull() { num = (l ? l->num : 0) + (r ? r->num : 0); }
} * root[N];
int n;
void build(node *&cur, int l, int r) {
  cur = new node;
  if (l == r) return;
  int mid = l + r >> 1;
  build(cur->l, l, mid);
  build(cur->r, mid + 1, r);
  return;
}
void init(int _n) {
  build(root[0], 1, n = _n);
  return;
}
void M(node *&cur, node *&lst, int i, int l = 1, int r = n) {
  cur = new node;
  cur->l = lst->l;
  cur->r = lst->r;
  cur->num = lst->num;
  if (l == r) {
    cur->num++;
    return;
  }
  int mid = l + r >> 1;
  if (i <= mid)
    M(cur->l, lst->l, i, l, mid);
  else
    M(cur->r, lst->r, i, mid + 1, r);
  return cur->pull();
}
void M(int i, int x) {
  M(root[i], root[i - 1], x);
  return;
}
int Q(node *&cur, int a, int b, int l = 1, int r = n) {
  if (!cur) return 0;
  if (a == l && b == r) return cur->num;
  int mid = l + r >> 1;
  if (a > mid) return Q(cur->r, a, b, mid + 1, r);
  if (b <= mid) return Q(cur->l, a, b, l, mid);
  return Q(cur->l, a, mid, l, mid) + Q(cur->r, mid + 1, b, mid + 1, r);
}
int Q(int a, int b, int l, int r) {
  if (l < 1) l = 1;
  return Q(root[b], l, r) - Q(root[a - 1], l, r);
}
}  // namespace PSEG
int n, m;
int h[N];
void TZL() {
  scf(n);
  for (int i = 0; i < (n); ++i) scf(h[i]);
  for (int i = (n - 1); i >= (1); --i) h[i] -= h[i - 1];
  h[n] = iinf;
  for (int i = (1); i <= (n - 1); ++i) h[n + i] = -h[i];
  m = n + n - 1;
  SA::init(m, h);
  PSEG::init(m);
  for (int i = (1); i <= (m); ++i) PSEG::M(i, SA::sa[i]);
  return;
}
void RANK1() {
  int q_n;
  scf(q_n);
  while (q_n--) {
    int l, r;
    scf(l, r);
    if (l == r) {
      printf("%d\n", n - 1);
      continue;
    }
    int lb = SA::LB(l + n, r - l);
    int rb = SA::RB(l + n, r - l);
    printf("%d\n", PSEG::Q(lb, rb, 1, n) - PSEG::Q(lb, rb, l - (r - l), r));
  }
  return;
}
int main() {
  TZL();
  RANK1();
  return 0;
}