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;
struct Node {
  int Left, Right, Sum;
  Node *Lc, *Rc;
};
int Get() {
  char c;
  while (c = getchar(), c < '0' || c > '9')
    ;
  int X = 0;
  while (c >= '0' && c <= '9') {
    X = X * 10 + c - 48;
    c = getchar();
  }
  return X;
}
Node *Build(int L, int R) {
  if (L > R) return NULL;
  Node *P = new Node;
  P->Left = L;
  P->Right = R;
  P->Sum = 0;
  if (L == R)
    P->Lc = P->Rc = NULL;
  else {
    int Mid = (L + R) / 2;
    P->Lc = Build(L, Mid);
    P->Rc = Build(Mid + 1, R);
  }
  return P;
}
Node *Insert(Node *P, int X) {
  Node *Q = new Node;
  *Q = *P;
  Q->Sum++;
  if (Q->Left != Q->Right)
    if (X <= (Q->Left + Q->Right) / 2)
      Q->Lc = Insert(Q->Lc, X);
    else
      Q->Rc = Insert(Q->Rc, X);
  return Q;
}
int GetSum(Node *P, int L, int R) {
  if (L > R) return 0;
  if (P->Left == L && P->Right == R) return P->Sum;
  int Mid = (P->Left + P->Right) / 2;
  if (R <= Mid) return GetSum(P->Lc, L, R);
  if (L > Mid) return GetSum(P->Rc, L, R);
  return GetSum(P->Lc, L, Mid) + GetSum(P->Rc, Mid + 1, R);
}
int main() {
  int N = Get() - 1;
  static int H[100000];
  for (int i = 0; i <= N; i++) H[i] = Get();
  static int S[200000];
  for (int i = 0; i < N; i++) S[i] = H[i + 1] - H[i];
  for (int i = 0; i < N; i++) S[N + i] = H[i] - H[i + 1];
  static int Data[200000];
  for (int i = 0; i < N * 2; i++) Data[i] = S[i];
  sort(Data, Data + N * 2);
  for (int i = 0; i < N * 2; i++) {
    int Left = 0, Right = N * 2 - 1;
    while (Left < Right) {
      int Mid = (Left + Right) / 2;
      if (Data[Mid] < S[i])
        Left = Mid + 1;
      else
        Right = Mid;
    }
    S[i] = Left;
  }
  static int Count[200002];
  memset(Count, 0, sizeof(Count));
  for (int i = 0; i < N * 2; i++) Count[S[i]] = 1;
  for (int i = 1; i < N * 2; i++) Count[i] += Count[i - 1];
  static int Rank[200000], SA[200000];
  for (int i = 0; i < N * 2; i++) Rank[i] = Count[S[i]] - 1;
  int Total = Count[N * 2 - 1], Step = 1;
  if (Total == N * 2)
    for (int i = 0; i < N * 2; i++) SA[Rank[i]] = i;
  while (Total < N * 2 && Step < N * 2) {
    static int X[200000], Y[200000];
    for (int i = 0; i < N * 2; i++) X[i] = Rank[i] + 2;
    for (int i = 0; i < N; i++)
      if (i + Step >= N)
        Y[i] = Y[N + i] = 1;
      else {
        Y[i] = Rank[i + Step] + 2;
        Y[N + i] = Rank[N + i + Step] + 2;
      }
    memset(Count, 0, sizeof(Count));
    for (int i = 0; i < N * 2; i++) Count[Y[i]]++;
    for (int i = 1; i <= N * 2 + 1; i++) Count[i] += Count[i - 1];
    static int Temp[200000];
    for (int i = 0; i < N * 2; i++) Temp[Count[Y[i] - 1]++] = i;
    memset(Count, 0, sizeof(Count));
    for (int i = 0; i < N * 2; i++) Count[X[i]]++;
    for (int i = 1; i <= N * 2 + 1; i++) Count[i] += Count[i - 1];
    for (int i = 0; i < N * 2; i++) SA[Count[X[Temp[i]] - 1]++] = Temp[i];
    Total = 0;
    for (int i = 0; i < N * 2; i++) {
      if (!i || X[SA[i]] != X[SA[i - 1]] || Y[SA[i]] != Y[SA[i - 1]]) Total++;
      Rank[SA[i]] = Total - 1;
    }
    Step *= 2;
  }
  for (int i = 0; i < N * 2; i++) Rank[SA[i]] = i;
  static int Height[200000];
  Height[0] = 0;
  for (int i = 0, P = 0; i < N * 2; i++)
    if (!Rank[i])
      P = 0;
    else {
      if (P) P--;
      int k = SA[Rank[i] - 1];
      int N0 = (i < N) ? N : (N * 2);
      int N1 = (k < N) ? N : (N * 2);
      while (i + P < N0 && k + P < N1 && S[i + P] == S[k + P]) P++;
      Height[Rank[i]] = P;
    }
  static int ST[18][200000];
  for (int i = 0; i < N * 2; i++) ST[0][i] = Height[i];
  for (int i = 1, L = 1; i < 18; i++, L *= 2)
    for (int j = 0; j < N * 2; j++)
      if (j < L)
        ST[i][j] = -1;
      else
        ST[i][j] = min(ST[i - 1][j], ST[i - 1][j - L]);
  static Node *Tree[200000];
  Tree[0] = Build(0, N - 1);
  for (int i = 0; i < N * 2; i++)
    if (SA[i] < N)
      Tree[i + 1] = Insert(Tree[i], SA[i]);
    else
      Tree[i + 1] = Tree[i];
  int Query = Get();
  while (Query--) {
    int L = Get() - 1, R = Get() - 1;
    if (L == R)
      printf("%d\n", N);
    else {
      int Left = Rank[L + N], Right = Rank[L + N];
      for (int i = 17; i >= 0; i--)
        if (ST[i][Left] >= R - L) Left -= 1 << i;
      for (int i = 17; i >= 0; i--)
        if (Right + (1 << i) < N * 2 && ST[i][Right + (1 << i)] >= R - L)
          Right += 1 << i;
      if (Left > Right)
        printf("0\n");
      else {
        int Ans = 0;
        Ans += GetSum(Tree[Right + 1], 0, 2 * L - R - 1);
        Ans -= GetSum(Tree[Left], 0, 2 * L - R - 1);
        Ans += GetSum(Tree[Right + 1], R + 1, N - 1);
        Ans -= GetSum(Tree[Left], R + 1, N - 1);
        printf("%d\n", Ans);
      }
    }
  }
  return 0;
}