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;
const double pi = acos(0) * 2;
template <class T>
inline T abs1(T a) {
  return a < 0 ? -a : a;
}
template <class T>
inline T max1(T a, T b) {
  return a > b ? a : b;
}
template <class T>
inline T min1(T a, T b) {
  return a < b ? a : b;
}
template <class T>
inline T gcd1(T a, T b) {
  if (a < b) swap(a, b);
  if (a % b == 0) return b;
  return gcd1(b, a % b);
}
template <class T>
inline T lb(T num) {
  return num & (-num);
}
template <class T>
inline int bitnum(T num) {
  int ans = 0;
  while (num) {
    num -= lb(num);
    ans++;
  }
  return ans;
}
long long pow(long long n, long long m, long long mod = 0) {
  long long ans = 1;
  long long k = n;
  while (m) {
    if (m & 1) {
      ans *= k;
      if (mod) ans %= mod;
    }
    k *= k;
    if (mod) k %= mod;
    m >>= 1;
  }
  return ans;
}
int wa[300100 * 2], wb[300100 * 2], wv[300100 * 2], ws1[300100 * 2];
int cmp(int *r, int a, int b, int l) {
  return r[a] == r[b] && r[a + l] == r[b + l];
}
void da(int *r, int *sa, int n, int m) {
  int i, j, p, *x = wa, *y = wb, *t;
  for (i = 0; i < m; i++) ws1[i] = 0;
  for (i = 0; i < n; i++) ws1[x[i] = r[i]]++;
  for (i = 1; i < m; i++) ws1[i] += ws1[i - 1];
  for (i = n - 1; i >= 0; i--) sa[--ws1[x[i]]] = i;
  for (j = 1, p = 1; p <= n; j *= 2, m = p) {
    for (p = 0, i = n - j; i < n; i++) y[p++] = i;
    for (i = 0; i < n; i++)
      if (sa[i] >= j) y[p++] = sa[i] - j;
    for (i = 0; i < n; i++) wv[i] = x[y[i]];
    for (i = 0; i < m; i++) ws1[i] = 0;
    for (i = 0; i < n; i++) ws1[wv[i]]++;
    for (i = 1; i < m; i++) ws1[i] += ws1[i - 1];
    for (i = n - 1; i >= 0; i--) sa[--ws1[wv[i]]] = y[i];
    for (t = x, x = y, y = t, p = 2, x[sa[0]] = 1, i = 1; i < n; i++)
      x[sa[i]] = cmp(y, sa[i - 1], sa[i], j) ? p - 1 : p++;
  }
  return;
}
template <class t>
struct tree_array {
  t num[300100 * 2], n;
  void upd(int no, t add) {
    while (no <= n) {
      num[no] += add;
      no += lb(no);
    }
  }
  t que(int no) {
    t ans = 0;
    while (no) {
      ans += num[no];
      no -= lb(no);
    }
    return ans;
  }
};
tree_array<int> tra_min, tra_max;
int n, orig[300100], delta[300100], suf1[300100], maxan1[300100];
map<int, int> mp;
int que[300100][2], lqu[300100];
int head[300100 * 2 + 10], nxt[300100 * 2 + 10], bound[300100 * 2 + 10][2];
int trans[300100 * 2];
int d[20];
int st[300100][20];
void InitRMQ(const int &n) {
  int i, j;
  for (d[0] = 1, i = 1; i < 21; ++i) d[i] = 2 * d[i - 1];
  for (i = 0; i < n; ++i) st[i][0] = maxan1[i];
  int k = int(log(double(n)) / log(2)) + 1;
  for (j = 1; j < k; ++j)
    for (i = 0; i < n; ++i) {
      if (i + d[j - 1] - 1 < n) {
        st[i][j] = min1(st[i][j - 1], st[i + d[j - 1]][j - 1]);
      } else
        break;
    }
}
inline int Query(int x, int y) {
  if (y < x) return -1;
  int k = int(log(double(y - x + 1)) / log(2.0));
  return min1(st[x][k], st[y - d[k] + 1][k]);
}
void calheight(int *r, int *sa, int *rank, int *height, int n) {
  int i, j, k = 0;
  for (i = 0; i < n; i++) rank[sa[i]] = i;
  for (i = 0; i < n; height[rank[i++] - 1] = k)
    for (k ? k-- : 0, j = sa[rank[i] - 1]; r[i + k] == r[j + k]; k++)
      ;
  return;
}
int main() {
  scanf("%d", &n);
  for (int i = 0; i < n; i++) scanf("%d", orig + i);
  for (int i = n - 2; i >= 0; i--) delta[i] = orig[i] - orig[i + 1];
  for (int i = n - 1; i < n - 1 + n - 1; i++) delta[i] = -delta[i - n + 1];
  int delta1[300100 * 2];
  memcpy(delta1, delta, sizeof(delta));
  sort(delta1, delta1 + 2 * n - 2);
  int n1 = 1;
  mp[delta1[0]] = n1++;
  for (int i = 1; i < 2 * n - 2; i++)
    if (delta1[i] != delta1[i - 1]) mp[delta1[i]] = n1++;
  int maxh = 2 * n1;
  for (int i = 0; i < 2 * n - 2; i++) delta1[i] = mp[delta[i]];
  delta1[2 * n - 2] = 0;
  da(delta1, suf1, 2 * n - 2, maxh);
  calheight(delta1, suf1, trans, maxan1, 2 * n - 2);
  for (int i = 0; i < 2 * n - 2; i++) trans[suf1[i]] = i;
  InitRMQ(2 * n - 2);
  memset(head, -1, sizeof(head));
  int nque;
  scanf("%d", &nque);
  for (int i1 = 0; i1 < nque; i1++) {
    int l, r;
    scanf("%d%d", &l, &r);
    l--;
    r--;
    lqu[i1] = r - l;
    if (l == r) {
      que[i1][1] = n - 1;
      continue;
    }
    int be = 0, en = n - 1;
    int be1 = 0, en1 = trans[l + n - 1] - 1;
    while (be1 < en1 - 1) {
      int mid = (be1 + en1) / 2;
      if (Query(mid, trans[l + n - 1] - 1) >= r - l)
        en1 = mid;
      else
        be1 = mid;
    }
    if (Query(be1, trans[l + n - 1] - 1) >= r - l)
      be = be1;
    else
      be = en1;
    while (Query(be, trans[l + n - 1] - 1) < r - l && be < trans[l + n - 1])
      be++;
    be1 = trans[l + n - 1], en1 = 2 * n - 2;
    while (be1 < en1 - 1) {
      int mid = (be1 + en1) / 2;
      if (Query(trans[l + n - 1], mid) >= r - l)
        be1 = mid;
      else
        en1 = mid;
    }
    if (Query(trans[l + n - 1], en1) >= r - l)
      en = en1;
    else
      en = be1;
    while (Query(trans[l + n - 1], en) < r - l && en > trans[l + n - 1] - 2)
      en--;
    be--;
    en++;
    nxt[i1] = head[be];
    head[be] = i1;
    nxt[i1 + 300100] = head[en];
    head[en] = i1 + 300100;
    bound[i1][1] = r + 2;
    bound[i1][0] = 2 * l - r;
    bound[i1 + 300100][1] = r + 2;
    bound[i1 + 300100][0] = 2 * l - r;
  }
  tra_max.n = tra_min.n = n + 1;
  for (int i = 0; i < (n - 1) * 2; i++) {
    if (suf1[i] < n - 1) {
      tra_min.upd(suf1[i] + 1, 1);
      tra_max.upd(n + 1 - suf1[i], 1);
    }
    while (head[i] != -1) {
      int a = 0;
      if (bound[head[i]][0] > 0) a += tra_min.que(bound[head[i]][0]);
      if (bound[head[i]][1] <= n - 1) {
        a += tra_max.que(n + 2 - bound[head[i]][1]);
        if (lqu[head[i] % 300100] >= 2)
          a -= tra_max.que(
              n + 2 - max1(n + 1 - lqu[head[i] % 300100], bound[head[i]][1]));
      }
      que[head[i] % 300100][head[i] / 300100] = a;
      head[i] = nxt[head[i]];
    }
  }
  for (int i = 0; i < nque; i++) printf("%d\n", que[i][1] - que[i][0]);
  return 0;
}