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>
void Get(int &T) {
  char C;
  bool F = 0;
  for (; C = getchar(), C < '0' || C > '9';)
    if (C == '-') F = 1;
  for (T = C - '0'; C = getchar(), C >= '0' && C <= '9'; T = T * 10 + C - '0')
    ;
  F && (T = -T);
}
int S[200005];
int N, M, K;
int L[200005], R[200005];
int Ans[200005];
void Init() {
  Get(N);
  for (int i = 1; i <= N; i++) {
    Get(S[i]);
  }
  N--;
  M = N;
  for (int i = 1; i <= N; i++) {
    S[i] = S[i + 1] - S[i];
  }
  S[++N] = 0x7f7f7f7f;
  for (int i = 1; i <= M; i++) S[++N] = -S[i];
  S[++N] = 0x7f7f7f7f + 1;
  Get(K);
  for (int i = 1; i <= K; i++) {
    Get(L[i]);
    Get(R[i]);
    R[i]--;
  }
}
int SA[200005];
int *Rank;
int WA[200005];
int WB[200005];
int WS[200005];
int WV[200005];
bool Cmp(int X, int Y) { return S[X] < S[Y]; }
int Log(int X) {
  int Ans = 0;
  if (X >> 16) {
    X >>= 16;
    Ans |= 16;
  }
  if (X >> 8) {
    X >>= 8;
    Ans |= 8;
  }
  if (X >> 4) {
    X >>= 4;
    Ans |= 4;
  }
  if (X >> 2) {
    X >>= 2;
    Ans |= 2;
  }
  if (X >> 1) {
    X >>= 1;
    Ans |= 1;
  }
  return Ans;
}
int Height[25][200005];
void SuffixArray() {
  for (int i = 1; i <= N; i++) SA[i] = i;
  std::sort(SA + 1, SA + N + 1, Cmp);
  for (int i = N; i >= 2; i--)
    if (S[SA[i]] != S[SA[i - 1]])
      S[SA[i]] = 1;
    else
      S[SA[i]] = 0;
  S[SA[1]] = 1;
  for (int i = 1, k = 0; i <= N; i++) {
    if (S[SA[i]] == 1) k++;
    S[SA[i]] = k;
  }
  int Alpha = S[SA[N]];
  int *X = WA, *Y = WB;
  for (int i = 1; i <= N; i++) X[i] = S[i];
  for (int Top = Alpha, i, L = 1, k; Top < N; L <<= 1) {
    k = Top;
    std::swap(X, Y);
    for (Top = 0, i = N - L + 1; i <= N; i++) {
      X[++Top] = i;
    }
    for (i = 1; i <= N; i++)
      if (SA[i] > L) {
        X[++Top] = SA[i] - L;
      }
    for (int i = 0; i <= k; i++) WS[i] = 0;
    for (int i = 1; i <= N; i++) WS[Y[X[i]]]++;
    for (int i = 1; i <= k; i++) WS[i] += WS[i - 1];
    for (int i = N; i >= 1; i--) SA[WS[Y[X[i]]]--] = X[i];
    Top = 1;
    X[SA[1]] = 1;
    for (int i = 2; i <= N; i++) {
      if ((Y[SA[i]] != Y[SA[i - 1]] || Y[SA[i] + L] != Y[SA[i - 1] + L])) Top++;
      X[SA[i]] = Top;
    }
  }
  Rank = X;
  for (int i = 1, H = 0; i <= N; i++) {
    if (Rank[i] == 1) {
      Height[0][Rank[i]] = H = 0;
    } else {
      for (H && (H--); S[i + H] == S[SA[Rank[i] - 1] + H]; H++)
        ;
      Height[0][Rank[i]] = H;
    }
  }
}
void ST() {
  for (int j = 1; (1 << j) <= N; j++) {
    for (int i = 1; i + (1 << j) - 1 <= N; i++) {
      Height[j][i] = Height[j - 1][i] < Height[j - 1][i + (1 << (j - 1))]
                         ? Height[j - 1][i]
                         : Height[j - 1][i + (1 << (j - 1))];
    }
  }
}
int LCP(int i, int j) {
  if (i == j) return 0x2f2f2f2f;
  i = Rank[i];
  j = Rank[j];
  if (i > j) std::swap(i, j);
  i++;
  int L = Log(j - i);
  return Height[L][i] < Height[L][j - (1 << L) + 1]
             ? Height[L][i]
             : Height[L][j - (1 << L) + 1];
}
int Bin1(int L, int R, int Pos, int X) {
  for (;;) {
    if (L == R) return L;
    if (L + 1 == R) {
      if (LCP(SA[L], Pos) >= X) return L;
      return R;
    }
    int Mid = L + R;
    Mid >>= 1;
    if (LCP(SA[Mid], Pos) >= X)
      R = Mid;
    else
      L = Mid;
  }
}
int Bin2(int L, int R, int Pos, int X) {
  for (;;) {
    if (L == R) return L;
    if (L + 1 == R) {
      if (LCP(SA[R], Pos) >= X) return R;
      return L;
    }
    int Mid = L + R;
    Mid >>= 1;
    if (LCP(SA[Mid], Pos) >= X)
      L = Mid;
    else
      R = Mid;
  }
}
struct Lis {
  int ID, K, Next;
} List[400005];
int ListTop;
int Head[200005];
void Add(int X, int K, int Y) {
  List[++ListTop] = (Lis){Y, K, Head[X]};
  Head[X] = ListTop;
}
int Tree[200005];
void Add(int X, int Delta) {
  for (; X <= N; X += X & -X) {
    Tree[X] += Delta;
  }
}
int Ask(int X) {
  int Ans = 0;
  for (; X >= 1; X -= X & -X) {
    Ans += Tree[X];
  }
  return Ans;
}
void Work() {
  SuffixArray();
  ST();
  for (int k = 1; k <= K; k++) {
    if (L[k] > R[k]) {
      Ans[k] = M;
      continue;
    }
    int RankL = Bin1(1, Rank[L[k]], L[k], R[k] - L[k] + 1);
    int RankR = Bin2(Rank[L[k]], N, L[k], R[k] - L[k] + 1);
    L[k] += M + 1;
    R[k] += M + 1;
    L[k] -= R[k] - L[k] + 1;
    R[k]++;
    Add(RankL - 1, -1, k);
    Add(RankR, 1, k);
  }
  for (int i = 1; i <= N; i++) {
    if (SA[i] > M) {
      Add(SA[i], 1);
    }
    for (int k = Head[i]; k; k = List[k].Next) {
      int Ans0;
      Ans0 = (Ask(N) - Ask(R[List[k].ID]) + Ask(L[List[k].ID] - 1)) * List[k].K;
      Ans[List[k].ID] += Ans0;
    }
  }
}
void Output() {
  for (int k = 1; k <= K; k++) {
    printf("%d\n", Ans[k]);
  }
}
int main() {
  Init();
  Work();
  Output();
  return 0;
}