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 <class T>
bool umin(T& a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T>
bool umax(T& a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
int arr[200009], S[200009 << 1][20], n, c[200009], ans[200009];
int ord[200009], q, LOG[200009], M[200009 << 1][20];
pair<int, int> C[200009];
bool cmp(int x, int y) { return (C[x] < C[y]); }
map<int, int> pm;
struct asd {
  int left, right;
  pair<int, int> dif;
} query[200009];
int rmq(int x, int y) {
  int k = LOG[y - x + 1];
  return min(M[x][k], M[y - (1 << k) + 1][k]);
}
int s[200009 << 2];
pair<int, int> bul(int x, int dis) {
  int st = 1, en = x - 1;
  int pos = x, pos1 = x - 1;
  while (st + 1 < en) {
    int mid = (st + en) >> 1;
    if (rmq(mid, x - 1) >= dis)
      en = mid;
    else
      st = mid;
  }
  for (int i = st; i <= en; i++)
    if (rmq(i, x - 1) >= dis) {
      pos = i;
      break;
    }
  st = x;
  en = n * 2 - 2;
  while (st + 1 < en) {
    int mid = (st + en) >> 1;
    if (rmq(x, mid) >= dis)
      st = mid;
    else
      en = mid;
  }
  for (int i = en; i >= st; i--)
    if (rmq(x, i) >= dis) {
      pos1 = i;
      break;
    }
  return make_pair(pos, pos1 + 1);
}
void upd(int p, int nd, int x, int y) {
  if (x == y) {
    s[nd]++;
    return;
  }
  int mid = (x + y) >> 1;
  if (p <= mid)
    upd(p, nd << 1, x, mid);
  else
    upd(p, nd << 1 | 1, mid + 1, y);
  s[nd] = s[nd << 1] + s[nd << 1 | 1];
}
int tap(int l, int r, int nd, int x, int y) {
  if (l > y or x > r) return 0;
  if (l <= x and y <= r) return s[nd];
  int mid = (x + y) >> 1;
  int i1 = tap(l, r, nd << 1, x, mid);
  int i2 = tap(l, r, nd << 1 | 1, mid + 1, y);
  return i2 + i1;
}
int go(int l, int r) {
  return tap(n + r + 1, n * 2, 1, 1, n * 2) +
         tap(n + 1, n + l - (r - l) - 1, 1, 1, n * 2);
}
vector<pair<pair<int, int>, int> > add[200009], rem[200009];
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) scanf("%d", arr + i);
  S[1][0] = -1000000007;
  for (int i = 2; i <= n; i++) S[i][0] = arr[i - 1] - arr[i];
  for (int i = n + 1; i < n * 2; i++) S[i][0] = arr[i - n + 1] - arr[i - n];
  for (int i = 1; i < n * 2; i++) {
    pm[S[i][0]] = 1;
    LOG[i] = log2(i);
  }
  int cnt = 0;
  for (typeof((pm).begin()) it = (pm).begin(); it != (pm).end(); it++)
    it->second = ++cnt;
  for (int i = 1; i < n * 2; i++) S[i][0] = pm[S[i][0]];
  for (int j = 1; j < 20; j++) {
    for (int i = 1; i < n * 2; i++)
      C[i] =
          make_pair(S[i][j - 1],
                    i + (1 << j - 1) < n * 2 ? S[i + (1 << j - 1)][j - 1] : -1),
      c[i] = i;
    sort(c + 1, c + (n * 2), cmp);
    for (int i = 1; i < n * 2; i++)
      S[c[i]][j] = S[c[i - 1]][j] + (C[c[i]] != C[c[i - 1]]);
  }
  for (int i = 1; i < (n * 2); i++) ord[S[i][19]] = i;
  for (int i = 1; i < n * 2 - 1; i++) {
    int a = ord[i];
    int b = ord[i + 1];
    for (int j = 19; j >= 0; j--)
      if (a < n * 2 and b < n * 2 and S[a][j] == S[b][j]) {
        a += (1 << j);
        b += (1 << j);
        M[i][0] += (1 << j);
      }
  }
  for (int j = 1; j < 20; j++)
    for (int i = 1; i < n * 2 - 1; i++)
      M[i][j] = min(M[i][j - 1], i + (1 << j - 1) < n * 2 - 1
                                     ? M[i + (1 << j - 1)][j - 1]
                                     : 1000000007);
  scanf("%d", &q);
  for (int i = 1; i <= q; i++) {
    int l, r;
    scanf("%d%d", &l, &r);
    if (l == r) {
      ans[i] = n - 1;
      continue;
    }
    pair<int, int> k = bul(S[l + 1][19], r - l);
    add[k.first].push_back(make_pair(make_pair(l, r), i));
    rem[k.second].push_back(make_pair(make_pair(l, r), i));
  }
  for (int i = 1; i < n * 2; i++) {
    for (typeof((add[i]).begin()) it = (add[i]).begin(); it != (add[i]).end();
         it++)
      ans[it->second] -= go(it->first.first, it->first.second);
    upd(ord[i], 1, 1, n * 2);
    for (typeof((rem[i]).begin()) it = (rem[i]).begin(); it != (rem[i]).end();
         it++)
      ans[it->second] += go(it->first.first, it->first.second);
  }
  for (int i = 1; i <= q; i++) printf("%d\n", ans[i]);
  return 0;
}