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>
#pragma GCC optimize("Ofast")
using namespace std;
template <typename T>
void read(T& x) {
  x = 0;
  char ch = getchar();
  long long f = 1;
  while (!isdigit(ch)) {
    if (ch == '-') f *= -1;
    ch = getchar();
  }
  while (isdigit(ch)) {
    x = x * 10 + ch - 48;
    ch = getchar();
  }
  x *= f;
}
struct custom_hash {
  static uint64_t splitmix64(uint64_t x) {
    x += 0x9e3779b97f4a7c15;
    x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
    x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
    return x ^ (x >> 31);
  }
  size_t operator()(uint64_t x) const {
    static const uint64_t FIXED_RANDOM =
        chrono::steady_clock::now().time_since_epoch().count();
    return splitmix64(x + FIXED_RANDOM);
  }
};
const int N = 300010;
int s[N], t[N];
int sa[N], rk[N << 1], oldrk[N << 1], cnt[N], id[N], tmp[N];
int height[N];
int n, m;
int tot;
int a[100010];
int f[200010][20];
map<int, int> mp;
struct p {
  int x, y;
  int val;
};
vector<p> add[200010], sub[200010];
int sum[200010];
int ans[100010];
bool cmp(int, int, int);
void suf_arr(int*, int*, int*, int*, int, int);
void init();
int cal(int, int);
int cal_lcp(int, int);
int findL(int, int);
int findR(int, int);
int low_bit(int);
void update(int, int);
int query(int);
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0), cout.tie(0);
  cin >> n;
  for (int i = 1; i <= n; ++i) {
    cin >> a[i];
    int x = a[i] - a[i - 1];
    int y = -x;
    if (mp.find(x) == mp.end()) mp[x] = ++tot;
    if (mp.find(y) == mp.end()) mp[y] = ++tot;
  }
  for (int i = 1; i <= n; ++i) {
    int x = a[i] - a[i - 1];
    s[++m] = mp[x];
  }
  s[++m] = 233333;
  for (int i = 1; i <= n; ++i) {
    int x = a[i - 1] - a[i];
    s[++m] = mp[x];
  }
  suf_arr(s, sa, rk, height, m, 233333);
  init();
  int q;
  int x, y;
  cin >> q;
  for (int i = 1; i <= q; ++i) {
    cin >> x >> y;
    if (x == y) {
      ans[i] = n - 1;
      continue;
    }
    int tmp = y - x;
    int l = findL(rk[x + 1 + n + 1], tmp);
    int r = findR(rk[x + 1 + n + 1], tmp);
    if (x - tmp > 1) {
      sub[l].push_back({2, x - tmp, i});
      add[r].push_back({2, x - tmp, i});
    }
    if (y + tmp < n) {
      sub[l].push_back({y + 2, n, i});
      add[r].push_back({y + 2, n, i});
    }
  }
  for (int i = 1; i <= m; ++i) {
    for (auto [x, y, id] : sub[i]) ans[id] -= query(y) - query(x - 1);
    update(sa[i], 1);
    for (auto [x, y, id] : add[i]) ans[id] += query(y) - query(x - 1);
  }
  for (int i = 1; i <= q; ++i) cout << ans[i] << '\n';
  return 0;
}
bool cmp(int x, int y, int w) {
  return oldrk[x] == oldrk[y] && oldrk[x + w] == oldrk[y + w];
}
void suf_arr(int* s, int* sa, int* rk, int* height, int n, int m) {
  int p;
  memset(cnt, 0, (m + 1) * sizeof(int));
  memset(rk, 0, (2 * n + 1) * sizeof(int));
  for (int i = 1; i <= n; ++i) ++cnt[rk[i] = s[i]];
  for (int i = 1; i <= m; ++i) cnt[i] += cnt[i - 1];
  for (int i = 1; i <= n; ++i) sa[cnt[rk[i]]--] = i;
  for (int w = 1, p = 0; w <= n; w <<= 1, m = p, p = 0) {
    for (int i = n - w + 1; i <= n; ++i) id[++p] = i;
    for (int i = 1; i <= n; ++i) {
      if (sa[i] > w) id[++p] = sa[i] - w;
    }
    memset(cnt, 0, (m + 1) * sizeof(int));
    for (int i = 1; i <= n; ++i) ++cnt[tmp[i] = rk[id[i]]];
    for (int i = 1; i <= m; ++i) cnt[i] += cnt[i - 1];
    for (int i = n; i; --i) sa[cnt[tmp[i]]--] = id[i];
    memcpy(oldrk, rk, (2 * n + 1) * sizeof(int));
    p = 0;
    for (int i = 1; i <= n; ++i) rk[sa[i]] = cmp(sa[i], sa[i - 1], w) ? p : ++p;
  }
  for (int i = 1, k = 0; i <= n; ++i) {
    if (k) --k;
    while (s[i + k] == s[sa[rk[i] - 1] + k]) ++k;
    height[rk[i]] = k;
  }
}
void init() {
  for (int i = 1; i <= m; ++i) f[i][0] = height[i];
  for (int j = 1, len = 2; j < 20; ++j, len <<= 1)
    for (int i = 1; i + len - 1 <= m; ++i)
      f[i][j] = min(f[i][j - 1], f[i + len / 2][j - 1]);
}
int cal(int l, int r) {
  int k = log2(r - l + 1), len = 1 << k;
  return min(f[l][k], f[r - len + 1][k]);
}
int cal_lcp(int x, int y) {
  if (x == y) return m - sa[x] + 1;
  if (x > y) swap(x, y);
  return cal(x + 1, y);
}
int findL(int x, int len) {
  int l = 1, r = x;
  while (l <= r) {
    int mid = l + r >> 1;
    if (cal_lcp(mid, x) >= len)
      r = mid - 1;
    else
      l = mid + 1;
  }
  return l;
}
int findR(int x, int len) {
  int l = x, r = m;
  while (l <= r) {
    int mid = l + r >> 1;
    if (cal_lcp(x, mid) < len)
      r = mid - 1;
    else
      l = mid + 1;
  }
  return l - 1;
}
int low_bit(int x) { return x & -x; }
void update(int i, int k) {
  while (i <= m) {
    sum[i] += k;
    i += low_bit(i);
  }
}
int query(int i) {
  int res = 0;
  while (i) {
    res += sum[i];
    i -= low_bit(i);
  }
  return res;
}