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;
int N;
int s[200000 + 10];
int sa[200000 + 10], sainv[200000 + 10], lcp[200000 + 10];
pair<pair<int, int>, int> p[200000 + 10], q[200000 + 10];
int freq[200000 + 10];
void radix_sort(void) {
  int i;
  for ((i) = 0; (i) < (int)(N + 5); (i)++) freq[i] = 0;
  for ((i) = 0; (i) < (int)(N); (i)++) freq[p[i].first.second + 2]++;
  for (i = 1; i < N + 5; i++) freq[i] += freq[i - 1];
  for ((i) = 0; (i) < (int)(N); (i)++) {
    q[freq[p[i].first.second + 1]] = p[i];
    freq[p[i].first.second + 1]++;
  }
  for ((i) = 0; (i) < (int)(N + 5); (i)++) freq[i] = 0;
  for ((i) = 0; (i) < (int)(N); (i)++) freq[q[i].first.first + 2]++;
  for (i = 1; i < N + 5; i++) freq[i] += freq[i - 1];
  for ((i) = 0; (i) < (int)(N); (i)++) {
    p[freq[q[i].first.first + 1]] = q[i];
    freq[q[i].first.first + 1]++;
  }
}
void suffix_array(void) {
  int i, j, tmp = 0;
  for ((i) = 0; (i) < (int)(N); (i)++) sainv[i] = s[i];
  for (int t = 1; t < N; t *= 2) {
    for ((i) = 0; (i) < (int)(N); (i)++)
      p[i] =
          make_pair(make_pair(sainv[i], ((i + t >= N) ? -1 : sainv[i + t])), i);
    radix_sort();
    int sz = 0;
    for ((i) = 0; (i) < (int)(N); (i)++) {
      if (i > 0 && p[i].first != p[i - 1].first) sz++;
      sainv[p[i].second] = sz;
    }
  }
  for ((i) = 0; (i) < (int)(N); (i)++) sa[sainv[i]] = i;
  for ((i) = 0; (i) < (int)(N); (i)++)
    if (sainv[i] != N - 1) {
      j = sa[sainv[i] + 1];
      while (i + tmp < N && j + tmp < N && s[i + tmp] == s[j + tmp]) tmp++;
      lcp[sainv[i]] = tmp;
      if (tmp > 0) tmp--;
    }
}
int lcpmin[20][200010];
void rmq(void) {
  int i, j;
  for ((i) = 0; (i) < (int)(N - 1); (i)++) lcpmin[0][i] = lcp[i];
  for ((i) = 0; (i) < (int)(17); (i)++) {
    int t = (1 << i);
    for ((j) = 0; (j) < (int)(N - 1); (j)++)
      lcpmin[i + 1][j] =
          min(lcpmin[i][j], ((j + t >= N - 1) ? 0 : lcpmin[i][j + t]));
  }
}
int tree[(1 << 18)];
void add(int x, int val) {
  for (int i = x; i < (1 << 18); i = ((i) | (i + 1))) tree[i] += val;
}
int sum(int x) {
  int ans = 0;
  for (int i = x; i > 0; i = ((i) & (i - 1))) ans += tree[i - 1];
  return ans;
}
vector<pair<pair<int, int>, pair<int, int> > > events;
int ans[100010];
void func(void) {
  int i;
  sort(events.begin(), events.end());
  int cur = 0;
  for ((i) = 0; (i) < (int)(events.size()); (i)++) {
    int x = events[i].first.first, y = events[i].first.second,
        id = events[i].second.first, coef = events[i].second.second;
    while (cur < x) {
      add(sa[cur], 1);
      cur++;
    }
    ans[id] += coef * sum(y);
  }
}
void query(int id, int x, int y, int coef) {
  events.push_back(make_pair(make_pair(x, y), make_pair(id, coef)));
}
void query(int id, int L, int R) {
  int i;
  int pos = sainv[L];
  int d = R - L;
  int low = pos;
  for (i = 17; i >= 0; i--) {
    int t = (1 << i);
    if (low - t >= 0 && lcpmin[i][low - t] >= d) low -= t;
  }
  int high = pos;
  for (i = 17; i >= 0; i--) {
    int t = (1 << i);
    if (lcpmin[i][high] >= d) high += t;
  }
  high++;
  if (N / 2 < N / 2 + L - d) {
    query(id, low, N / 2, 1);
    query(id, low, N / 2 + L - d, -1);
    query(id, high, N / 2, -1);
    query(id, high, N / 2 + L - d, 1);
  }
  if (N / 2 + R + 1 < N - d + 1) {
    query(id, low, N / 2 + R + 1, 1);
    query(id, low, N - d + 1, -1);
    query(id, high, N / 2 + R + 1, -1);
    query(id, high, N - d + 1, 1);
  }
}
int h[200010];
pair<int, int> r[200010];
int main(void) {
  int M, Q, L, R, i;
  scanf("%d", &M);
  for ((i) = 0; (i) < (int)(M); (i)++) scanf("%d", &h[i]);
  M--;
  for ((i) = 0; (i) < (int)(M); (i)++) h[i] -= h[i + 1];
  for ((i) = 0; (i) < (int)(M); (i)++) h[M + i] = -h[i];
  N = 2 * M;
  for ((i) = 0; (i) < (int)(N); (i)++) r[i] = make_pair(h[i], i);
  sort(r, r + N);
  int tmp = 0;
  for ((i) = 0; (i) < (int)(N); (i)++) {
    if (i > 0 && r[i].first != r[i - 1].first) tmp++;
    s[r[i].second] = tmp;
  }
  suffix_array();
  rmq();
  scanf("%d", &Q);
  for ((i) = 0; (i) < (int)(Q); (i)++) {
    scanf("%d%d", &L, &R);
    L--;
    R--;
    if (L == R) {
      ans[i] = M;
    } else {
      query(i, L, R);
    }
  }
  func();
  for ((i) = 0; (i) < (int)(Q); (i)++) printf("%d\n", ans[i]);
  return 0;
}