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 Get_Int() {
  int Num = 0, Flag = 1;
  char ch;
  do {
    ch = getchar();
    if (ch == '-') Flag *= -1;
  } while (ch < '0' || ch > '9');
  do {
    Num = Num * 10 + ch - '0';
    ch = getchar();
  } while (ch >= '0' && ch <= '9');
  return Num * Flag;
}
int N, Limit, M;
int A[200005], Bucket[200005], SA[200005], SA1[200005], Rank[200005],
    Rank1[200005], Height[200005];
int Min[200005][20];
inline void ST() {
  for (int i = 1; i <= N; ++i) Min[i][0] = Height[i];
  for (int j = 1; j < 20; ++j)
    for (int i = 1; i <= N; ++i)
      if (i + (1 << j) - 1 <= N)
        Min[i][j] = min(Min[i][j - 1], Min[i + (1 << j - 1)][j - 1]);
}
namespace Persistant_Segment_Tree {
class Node {
 public:
  int Sum;
  Node *Child[2];
} Nodes[200005 * 20];
int Total, Size;
Node *Root[200005];
Node *Modify(Node *x, int Location, int Value, int Left, int Right) {
  Node *New = &Nodes[Total++];
  New->Sum = (x ? x->Sum : 0) + Value;
  if (Left == Right) return New;
  int Mid = Left + Right >> 1, i = Location > Mid;
  New->Child[i] = Modify((x ? x->Child[i] : 0), Location, Value,
                         (i ? Mid + 1 : Left), (i ? Right : Mid));
  New->Child[i ^ 1] = (x ? x->Child[i ^ 1] : 0);
  return New;
}
int Query(Node *x, int left, int right, int Left, int Right) {
  if (!x || left > right) return 0;
  if (left == Left && right == Right) return x->Sum;
  int Mid = Left + Right >> 1;
  if (left > Mid || right <= Mid) {
    int i = left > Mid;
    return Query(x->Child[i], left, right, (i ? Mid + 1 : Left),
                 (i ? Right : Mid));
  }
  return Query(x->Child[0], left, Mid, (0 ? Mid + 1 : Left),
               (0 ? Right : Mid)) +
         Query(x->Child[1], Mid + 1, right, (1 ? Mid + 1 : Left),
               (1 ? Right : Mid));
}
int Query(int x1, int x2, int y1, int y2) {
  if (x1 > x2) return 0;
  return Query(Root[x2], y1, y2, 1, N) - Query(Root[x1 - 1], y1, y2, 1, N);
}
inline void Build() {
  Size = N >> 1;
  for (int i = 1; i < Size; ++i)
    Root[i] = Modify(Root[i - 1], Rank[i + Size], 1, 1, N);
}
inline int Solve(int Left, int Right) {
  if (Left > Right) return Size - 1;
  int x = Rank[Left], Length = Right - Left + 1, L = x + 1, R = x + 1;
  for (int i = 19; i >= 0; --i) {
    if (L - (1 << i) > 0 && Min[L - (1 << i)][i] >= Length) L -= 1 << i;
    if (Min[R][i] >= Length) R += 1 << i;
  }
  return Query(1, Left - 1 - Length, L - 1, R - 1) +
         Query(Right + 2, Size - 1, L - 1, R - 1);
}
inline void Solve() {
  cin >> M;
  ST();
  while (M--) {
    int Left = Get_Int(), Right = Get_Int();
    printf("%d\n", Solve(Left, Right - 1));
  }
}
}  // namespace Persistant_Segment_Tree
inline void Difference() {
  for (int i = 1; i < N; ++i) {
    A[i] = A[i + 1] - A[i];
    A[i + N] = -A[i];
  }
  A[N] = A[N * 2] = 0x3F3F3F3F;
}
inline void Discretization() {
  int Total = 0;
  for (int i = 1; i <= N << 1; ++i) Bucket[++Total] = A[i];
  sort(Bucket + 1, Bucket + Total + 1);
  Total = unique(Bucket + 1, Bucket + Total + 1) - Bucket - 1;
  for (int i = 1; i <= N << 1; ++i)
    A[i] = lower_bound(Bucket + 1, Bucket + Total + 1, A[i]) - Bucket;
}
inline void Calculate_SA() {
  N <<= 1;
  Limit = N;
  for (int i = 0; i <= Limit; ++i) Bucket[i] = 0;
  for (int i = 1; i <= N; ++i) ++Bucket[Rank[i] = A[i]];
  for (int i = 1; i <= Limit; ++i) Bucket[i] += Bucket[i - 1];
  for (int i = N; i; --i) SA[Bucket[Rank[i]]--] = i;
  for (int j = 1, k = 0; k < N; j <<= 1, Limit = k) {
    k = 0;
    for (int i = N - j + 1; i <= N; ++i) SA1[++k] = i;
    for (int i = 1; i <= N; ++i)
      if (SA[i] > j) SA1[++k] = SA[i] - j;
    swap(Rank, Rank1);
    for (int i = 1; i <= N; ++i) Rank[i] = Rank1[SA1[i]];
    for (int i = 0; i <= Limit; ++i) Bucket[i] = 0;
    for (int i = 1; i <= N; ++i) ++Bucket[Rank[i]];
    for (int i = 1; i <= N; ++i) Bucket[i] += Bucket[i - 1];
    for (int i = N; i; --i) SA[Bucket[Rank[i]]--] = SA1[i];
    k = 0;
    for (int i = 1; i <= N; ++i)
      k = max(k, Rank[SA[i]] = Rank[SA[i - 1]] +
                               (Rank1[SA[i]] != Rank1[SA[i - 1]] ||
                                Rank1[SA[i] + j] != Rank1[SA[i - 1] + j]));
  }
}
inline void Calculate_Height() {
  int k = 0;
  for (int i = 1; i < N; ++i) {
    int j = SA[Rank[i] - 1];
    while (A[j + k] == A[i + k]) ++k;
    Height[Rank[i]] = k;
    if (k) --k;
  }
}
int main() {
  cin >> N;
  for (int i = 1; i <= N; ++i) A[i] = Get_Int();
  Difference();
  Discretization();
  Calculate_SA();
  Calculate_Height();
  Persistant_Segment_Tree::Build();
  Persistant_Segment_Tree::Solve();
  return 0;
}