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;
inline int read() {
  char ch = getchar();
  int x = 0, f = 1;
  while (ch < '0' || ch > '9') {
    if (ch == '-') f = -1;
    ch = getchar();
  }
  while ('0' <= ch && ch <= '9') {
    x = x * 10 + ch - '0';
    ch = getchar();
  }
  return x * f;
}
inline long long llread() {
  char ch = getchar();
  long long x = 0, f = 1;
  while (ch < '0' || ch > '9') {
    if (ch == '-') f = -1;
    ch = getchar();
  }
  while ('0' <= ch && ch <= '9') {
    x = x * 10 + ch - '0';
    ch = getchar();
  }
  return x * f;
}
int ch[200010], a[200010], c[200010], x[200010], y[200010];
int sa[200010], Rank[200010], height[200010];
int Log[200010], mn[200010][20];
inline void get_sa(int n, int m) {
  for (int i = (1); i <= (m); i++) c[i] = 0;
  for (int i = (1); i <= (n); i++) c[x[i] = ch[i]]++;
  for (int i = (2); i <= (m); i++) c[i] += c[i - 1];
  for (int i = (n); i >= (1); i--) sa[c[x[i]]--] = i;
  for (int k = 1; k <= n; k <<= 1) {
    int tot = 0;
    for (int i = (n - k + 1); i <= (n); i++) y[++tot] = i;
    for (int i = (1); i <= (n); i++)
      if (sa[i] > k) y[++tot] = sa[i] - k;
    for (int i = (1); i <= (m); i++) c[i] = 0;
    for (int i = (1); i <= (n); i++) c[x[y[i]]]++;
    for (int i = (2); i <= (m); i++) c[i] += c[i - 1];
    for (int i = (n); i >= (1); i--) sa[c[x[y[i]]]--] = y[i];
    swap(x, y);
    x[sa[1]] = 1;
    tot = 1;
    for (int i = (2); i <= (n); i++) {
      x[sa[i]] = (y[sa[i]] == y[sa[i - 1]] && y[sa[i] + k] == y[sa[i - 1] + k]
                      ? tot
                      : ++tot);
    }
    if (tot >= n) break;
    m = tot;
  }
}
inline void get_height(int n) {
  for (int i = (1); i <= (n); i++) Rank[sa[i]] = i;
  int k = 0;
  for (int i = (1); i <= (n); i++) {
    if (!Rank[i]) continue;
    if (k) k--;
    int j = sa[Rank[i] - 1];
    while (ch[i + k] == ch[j + k]) k++;
    height[Rank[i]] = k;
  }
}
inline void get_rmq(int n) {
  Log[1] = 0;
  for (int i = (2); i <= (n); i++) Log[i] = Log[i / 2] + 1;
  for (int i = (1); i <= (n); i++) mn[i][0] = height[i];
  for (int i = (1); i <= (19); i++)
    for (int j = (1); j <= (n); j++) {
      if (j + (1 << i - 1) <= n)
        mn[j][i] = min(mn[j][i - 1], mn[j + (1 << i - 1)][i - 1]);
    }
}
inline int get_mn(int l, int r) {
  int i = Log[r - l + 1];
  return min(mn[l][i], mn[r - (1 << i) + 1][i]);
}
struct que {
  int id, l, r;
} qt[200010];
inline int cmp0(que xx, que yy) { return xx.l * 2 - xx.r < yy.l * 2 - yy.r; }
inline int cmp1(que xx, que yy) { return xx.r < yy.r; }
int tt;
inline void add(int x, int y) {
  for (; x <= tt; x += (x & -x)) c[x] += y;
}
inline int query(int x) {
  int ret = 0;
  for (; x > 0; x -= (x & -x)) ret += c[x];
  return ret;
}
inline int calc(int pos, int len) {
  int l, r, left = pos + 1, right = pos - 1;
  l = 1, r = pos;
  while (l <= r) {
    int mid = (l + r) / 2;
    if (get_mn(mid, pos) >= len)
      r = (left = mid) - 1;
    else
      l = mid + 1;
  }
  l = pos + 1, r = tt;
  while (l <= r) {
    int mid = (l + r) / 2;
    if (get_mn(pos + 1, mid) >= len)
      l = (right = mid) + 1;
    else
      r = mid - 1;
  }
  return query(right) - query(left - 2);
}
int ans[200010];
int main() {
  int n = read();
  for (int i = (1); i <= (n); i++) a[i] = read();
  for (int i = (1); i < (n); i++) ch[++tt] = a[i + 1] - a[i];
  ch[++tt] = 2147483647;
  for (int i = (1); i < (n); i++) ch[++tt] = a[i] - a[i + 1];
  ch[++tt] = 2147483647;
  for (int i = (1); i <= (tt); i++) c[i] = ch[i];
  sort(c + 1, c + tt + 1);
  int tot = unique(c + 1, c + tt + 1) - c - 1;
  for (int i = (1); i <= (tt); i++)
    ch[i] = lower_bound(c + 1, c + tot + 1, ch[i]) - c;
  get_sa(tt, tot);
  get_height(tt);
  get_rmq(tt);
  int Q = read();
  for (int i = (1); i <= (Q); i++) qt[i] = (que){i, read(), read()};
  sort(qt + 1, qt + Q + 1, cmp0);
  memset(c, 0, sizeof(c));
  int now = 1;
  for (int i = (1); i <= (Q); i++) {
    while (now < n && now < 2 * qt[i].l - qt[i].r) add(Rank[now + n], 1), now++;
    ans[qt[i].id] += calc(Rank[qt[i].l], qt[i].r - qt[i].l);
  }
  sort(qt + 1, qt + Q + 1, cmp1);
  memset(c, 0, sizeof(c));
  now = n - 1;
  for (int i = (Q); i >= (1); i--) {
    while (now > 0 && now > qt[i].r) add(Rank[now + n], 1), now--;
    ans[qt[i].id] += calc(Rank[qt[i].l], qt[i].r - qt[i].l);
  }
  for (int i = (1); i <= (Q); i++)
    if (qt[i].l == qt[i].r) ans[qt[i].id] = n - 1;
  for (int i = (1); i <= (Q); i++) printf("%d\n", ans[i]);
  return 0;
}