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>
void get_int(T &x) {
  char t = getchar();
  bool neg = false;
  x = 0;
  for (; (t > '9' || t < '0') && t != '-'; t = getchar())
    ;
  if (t == '-') neg = true, t = getchar();
  for (; t <= '9' && t >= '0'; t = getchar()) x = x * 10 + t - '0';
  if (neg) x = -x;
}
template <typename T>
void print_int(T x) {
  if (x < 0) putchar('-'), x = -x;
  short a[20] = {}, sz = 0;
  while (x > 0) a[sz++] = x % 10, x /= 10;
  if (sz == 0) putchar('0');
  for (int i = sz - 1; i >= 0; i--) putchar('0' + a[i]);
}
const int inf = 0x3f3f3f3f;
const long long Linf = 1ll << 61;
const double pi = acos(-1.0);
const int maxn = 200111;
struct suffix_array {
  int sa[maxn], t[maxn], t2[maxn], c[maxn], s[maxn], n;
  int rk[maxn], v[20][maxn], lg[maxn];
  void construct_sa() {
    vector<int> all;
    for (int i = 0; i < n; i++) all.push_back(s[i]);
    sort(all.begin(), all.end());
    all.erase(unique(all.begin(), all.end()), all.end());
    int m = all.size();
    for (int i = 0; i < n; i++)
      s[i] = lower_bound(all.begin(), all.end(), s[i]) - all.begin();
    int *x = t, *y = t2;
    for (int i = 0; i < m; i++) c[i] = 0;
    for (int i = 0; i < n; i++) c[x[i] = s[i]]++;
    for (int i = 1; i < m; i++) c[i] += c[i - 1];
    for (int i = n - 1; i >= 0; i--) sa[--c[x[i]]] = i;
    for (int k = 1; k <= n; k <<= 1) {
      int p = 0;
      for (int i = n - k; i < n; i++) y[p++] = i;
      for (int i = 0; i < n; i++)
        if (sa[i] >= k) y[p++] = sa[i] - k;
      for (int i = 0; i < m; i++) c[i] = 0;
      for (int i = 0; i < n; i++) c[x[y[i]]]++;
      for (int i = 1; i < m; i++) c[i] += c[i - 1];
      for (int i = n - 1; i >= 0; i--) sa[--c[x[y[i]]]] = y[i];
      swap(x, y);
      x[sa[0]] = 0;
      p = 1;
      for (int i = 1; i < n; i++)
        x[sa[i]] =
            (y[sa[i]] == y[sa[i - 1]] && y[sa[i] + k] == y[sa[i - 1] + k])
                ? p - 1
                : p++;
      if (p >= n) break;
      m = p;
    }
  }
  void construct_lcp() {
    int k = 0;
    for (int i = 0; i < n; i++) rk[sa[i]] = i;
    for (int i = 0; i < n - 1; i++) {
      if (k) k--;
      int j = sa[rk[i] - 1];
      while (s[i + k] == s[j + k]) k++;
      v[0][rk[i]] = k;
    }
    for (int i = 2; i < n; i++) lg[i] = lg[i >> 1] + 1;
    for (int i = 1; i < 20; i++) {
      for (int j = 2; j < n; j++) {
        int nxt = j + (1 << i - 1);
        if (nxt >= n)
          v[i][j] = v[i - 1][j];
        else
          v[i][j] = min(v[i - 1][j], v[i - 1][nxt]);
      }
    }
  }
  int qmin(int l, int r) {
    int t = lg[r - l + 1];
    return min(v[t][l], v[t][r - (1 << t) + 1]);
  }
  int qlcp(int l, int r) {
    if (l == r) return n - l;
    l = rk[l];
    r = rk[r];
    if (l > r) swap(l, r);
    return qmin(l + 1, r);
  }
  void get_bound(int v, int len, int &lb, int &ub) {
    v = rk[v];
    int l = 0, r = v, mid;
    while (l < r - 1) {
      mid = (l + r) >> 1;
      if (qlcp(sa[mid], sa[v]) >= len)
        r = mid;
      else
        l = mid;
    }
    lb = r;
    l = v;
    r = n;
    while (l < r - 1) {
      mid = (l + r) >> 1;
      if (qlcp(sa[mid], sa[v]) >= len)
        l = mid;
      else
        r = mid;
    }
    ub = l;
  }
  struct data {
    int id, l, r, coef;
    data(int id, int l, int r, int coef) {
      this->id = id;
      this->l = l;
      this->r = r;
      this->coef = coef;
    }
  };
  vector<data> q[maxn];
  int bit[maxn];
  void add(int x, int v) {
    for (; x <= n; x += x & -x) bit[x] += v;
  }
  int query(int x) {
    int ret = 0;
    for (; x; x -= x & -x) ret += bit[x];
    return ret;
  }
  void addquery(int id, int pos, int l, int r, int coef) {
    q[pos].push_back(data(id, l, r, coef));
  }
  void solve(int ans[]) {
    for (int i = 0; i < n; i++) {
      add(sa[i] + 1, 1);
      for (int j = 0; j < (int)q[i].size(); j++) {
        data &cur = q[i][j];
        ans[cur.id] += cur.coef * (query(cur.r + 1) - query(cur.l));
      }
    }
  }
} sa;
int n, q, h[100111], a[100111], b[100111], ans[100111];
int main() {
  get_int(n);
  for (int i = 0; i < n; i++) get_int(h[i]);
  sa.n = n + n;
  for (int i = 0; i < n - 1; i++) sa.s[i] = h[i + 1] - h[i];
  sa.s[n - 1] = -inf;
  for (int i = 0; i < n - 1; i++) sa.s[i + n] = h[i] - h[i + 1];
  sa.s[n + n - 1] = -inf - 1;
  sa.construct_sa();
  sa.construct_lcp();
  get_int(q);
  for (int i = 1, l, r; i <= q; i++) {
    get_int(a[i]), get_int(b[i]);
    if (a[i] == b[i]) {
      ans[i] = n - 1;
      continue;
    }
    a[i]--;
    b[i]--;
    sa.get_bound(a[i], b[i] - a[i], l, r);
    if (a[i] + a[i] - 1 >= b[i]) {
      sa.addquery(i, l - 1, n, n + a[i] + a[i] - 1 - b[i], -1);
      sa.addquery(i, r, n, n + a[i] + a[i] - 1 - b[i], 1);
    }
    if (b[i] < n - 2) {
      sa.addquery(i, l - 1, n + b[i] + 1, n + n - 2, -1);
      sa.addquery(i, r, n + b[i] + 1, n + n - 2, 1);
    }
  }
  sa.solve(ans);
  for (int i = 1; i <= q; i++) printf("%d\n", ans[i]);
  return 0;
}