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 maxn = 200010, mo = 1e9 + 7;
const double LOG = log(2);
int n, q, l, r, s[maxn], sa[maxn], rk[maxn], h[maxn], cnt, LG[maxn];
long long e[maxn], ha[maxn * 2][22];
int rmq[maxn][22], ex[22];
vector<int> T[maxn];
void Init() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) scanf("%d", s + i);
  for (int i = 1; i < n; i++) s[i] = s[i + 1] - s[i];
  s[n] = 1e9 + 1;
  for (int i = n + 1; i < n + n; i++) s[i] = -s[i - n];
  s[n + n] = -s[n];
  ex[0] = 1;
  for (int i = 1; i < 22; i++) ex[i] = ex[i - 1] << 1;
  for (int i = 1; i < maxn; i++) LG[i] = log(i) / LOG;
}
inline bool cmp(int x, int y) {
  for (int k = LG[n + n - max(x, y)]; k >= 0; k--)
    if (ha[x][k] == ha[y][k]) x += ex[k], y += ex[k];
  return s[x] == s[y] ? s[x + 1] < s[y + 1] : s[x] < s[y];
}
void Suffix_Array() {
  e[0] = 1;
  for (int i = 1; i <= n + n; i++) e[i] = (e[i - 1] * 97) % mo, ha[i][0] = s[i];
  for (int k = 1, L = 1; L <= n + n; k++, L <<= 1)
    for (int i = 1; i <= n + n; i++)
      ha[i][k] = ((long long)ha[i][k - 1] * e[L] + ha[i + L][k - 1]) % mo;
  for (int i = 1; i < n + n; i++) sa[i] = i;
  sort(sa + 1, sa + n + n, cmp);
  for (int i = 1; i < n + n; i++) rk[sa[i]] = i;
  for (int i = 1; i < n + n; i++) {
    if (rk[i] == 1) continue;
    int len = max(0, h[rk[i - 1]] - 1);
    while (s[i + len] == s[sa[rk[i] - 1] + len]) len++;
    h[rk[i]] = len;
  }
}
inline int rmq_query(int l, int r) {
  int e = log(r - l + 1) / LOG;
  if (ex[e] > r) e--;
  return min(rmq[l][e], rmq[r - ex[e] + 1][e]);
}
void RMQ() {
  for (int i = 1; i < n + n; i++) rmq[i][0] = h[i];
  for (int e = 1, range = 1; range < n + n; e++, range <<= 1)
    for (int i = 1; i < n + n - range; i++)
      rmq[i][e] = min(rmq[i][e - 1], rmq[i + range][e - 1]);
}
inline int ask(int x, int val) {
  int ret = 0;
  for (; x > 0; x -= x & -x)
    ret += lower_bound(T[x].begin(), T[x].end(), val) - T[x].begin();
  return ret;
}
void Answer() {
  for (int i = 1; i < n + n; i++)
    for (int j = i; j > i - (i & -i); j--)
      if (sa[j] > n) T[i].push_back(sa[j] - n);
  for (int i = 1; i < n + n; i++) sort(T[i].begin(), T[i].end());
  for (int i = 1; i < n + n; i++) s[i] = s[i - 1] + (sa[i] > n);
  for (scanf("%d", &q); q--;) {
    scanf("%d%d", &l, &r);
    r--;
    if (l > r) {
      printf("%d\n", n - 1);
      continue;
    }
    if ((r - l + 2) * 2 > n) {
      printf("0\n");
      continue;
    }
    int vl = 0, vr = rk[l] - 1, pos = rk[l], len = r - l + 1, rl, rr;
    while (vl < vr) {
      int m = (vl + vr + 1) >> 1;
      if (rmq_query(pos - m + 1, pos) >= len)
        vl = m;
      else
        vr = m - 1;
    }
    rl = pos - vl;
    vl = 0;
    vr = n + n - rk[l] - 1;
    while (vl < vr) {
      int m = (vl + vr + 1) >> 1;
      if (rmq_query(pos + 1, pos + m) >= len)
        vl = m;
      else
        vr = m - 1;
    }
    rr = pos + vl;
    printf("%d\n", ask(rr, l - len) - ask(rl - 1, l - len) + s[rr] - s[rl - 1] -
                       ask(rr, r + 2) + ask(rl - 1, r + 2));
  }
}
int main() {
  Init();
  Suffix_Array();
  RMQ();
  Answer();
}