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 <typename T>
inline T Abs(T x) {
  return (x >= 0) ? x : -x;
}
template <typename T>
inline T sqr(T x) {
  return x * x;
}
template <typename T>
string toStr(T x) {
  stringstream st;
  st << x;
  string s;
  st >> s;
  return s;
}
const int INF = (int)1E9;
const long long INF64 = (long long)1E18;
const long double EPS = 1E-9;
const long double PI = 3.1415926535897932384626433832795;
const int MAXN = 200100;
const int OSN = (1 << 18) - 1;
const long long MOD = INF + 19;
struct state {
  int a, b, id;
  state(int a = 0, int b = 0, int id = 0) : a(a), b(b), id(id) {}
  bool operator<(const state &s) const {
    if (a != s.a) return a < s.a;
    return b < s.b;
  }
  bool operator!=(const state &s) const { return a != s.a || b != s.b; }
};
int n, a[MAXN], s1[MAXN], s2[MAXN], p[MAXN];
long long h1[MAXN], h2[MAXN], pw[2 * MAXN + 200];
vector<int> t[2 * OSN + 10];
state v[MAXN];
inline int get(const vector<int> &a, int l, int r) {
  return (int)(upper_bound((a).begin(), (a).end(), r) -
               lower_bound((a).begin(), (a).end(), l));
}
inline int get(int v, int l, int r, int ll, int rr, int L, int R) {
  if (l > rr || ll > r || ll > rr) return 0;
  if (ll <= l && r <= rr) return get(t[v], L, R);
  int mid = (l + r) >> 1;
  return get(2 * v + 1, l, mid, ll, rr, L, R) +
         get(2 * v + 2, mid + 1, r, ll, rr, L, R);
}
inline void build(const vector<pair<int, int> > &a, int v, int l, int r) {
  if (l == r) {
    if (l < (int)a.size()) t[v].push_back(a[l].second);
  } else {
    int mid = (l + r) >> 1;
    build(a, 2 * v + 1, l, mid);
    build(a, 2 * v + 2, mid + 1, r);
    vector<int> &v1 = t[2 * v + 1];
    vector<int> &v2 = t[2 * v + 2];
    t[v].resize(v1.size() + v2.size());
    merge((v1).begin(), (v1).end(), (v2).begin(), (v2).end(), t[v].begin());
  }
}
inline long long getint64(long long *h, int l, int r) {
  long long ans = h[r];
  if (l > 0) ans = ans - h[l - 1];
  return ans * pw[MAXN - l];
}
inline void calcint64es(int *s, long long *h) {
  for (int i = 0; i < (int)(2 * n - 1); i++) {
    h[i] = pw[i] * s[i];
    if (i > 0) h[i] = h[i] + h[i - 1];
  }
}
inline int compare(int p1, int p2, int len) {
  int l = 1, r = len;
  while (r - l > 1) {
    int mid = (l + r) >> 1;
    if (getint64(h1, p1, p1 + mid - 1) == getint64(h2, p2, p2 + mid - 1))
      l = mid;
    else
      r = mid;
  }
  int lcp = 0;
  for (int mid = l; mid <= r; mid++)
    if (getint64(h1, p1, p1 + mid - 1) == getint64(h2, p2, p2 + mid - 1))
      lcp = mid;
  if (lcp == len) return 0;
  return (s1[p1 + lcp] < s2[p2 + lcp]) ? -1 : +1;
}
inline int findFirst(int x, int len) {
  int l = 0, r = n - 2;
  while (r - l > 1) {
    int mid = (l + r) >> 1;
    if (compare(p[mid], x, len) < 0)
      l = mid;
    else
      r = mid;
  }
  for (int mid = l; mid <= r; mid++)
    if (compare(p[mid], x, len) == 0) return mid;
  return n;
}
inline int findLast(int x, int len) {
  int l = 0, r = n - 2;
  while (r - l > 1) {
    int mid = (l + r) >> 1;
    if (compare(p[mid], x, len) > 0)
      r = mid;
    else
      l = mid;
  }
  for (int mid = r; mid >= l; mid--)
    if (compare(p[mid], x, len) == 0) return mid;
  return n - 1;
}
inline pair<int, int> findPos(int x, int len) {
  return make_pair(findFirst(x, len), findLast(x, len));
}
int main() {
  for (int i = 0; i < (int)(2 * MAXN + 100); i++)
    if (i == 0)
      pw[i] = 1;
    else
      pw[i] = pw[i - 1] * (INF + 7);
  cin >> n;
  for (int i = 0; i < (int)(n); i++) scanf("%d", &a[i]);
  for (int i = 0; i < (int)(n - 1); i++) {
    s1[i + n - 1] = s1[i] = a[i + 1] - a[i];
    s2[i + n - 1] = s2[i] = a[i] - a[i + 1];
  }
  calcint64es(s1, h1);
  calcint64es(s2, h2);
  vector<int> X;
  for (int i = 0; i < (int)(n - 1); i++) X.push_back(s1[i]);
  sort((X).begin(), (X).end());
  X.erase(unique((X).begin(), (X).end()), X.end());
  for (int i = 0; i < (int)(n - 1); i++)
    p[i] = (int)(lower_bound((X).begin(), (X).end(), s1[i]) - X.begin());
  int k = 2;
  while (k <= n) {
    for (int i = 0; i < (int)(n - 1); i++)
      v[i] = state(p[i], p[(i + k / 2) % (n - 1)], i);
    int cur = 0;
    sort(v, v + n - 1);
    for (int i = 0; i < (int)(n - 1); i++) {
      p[v[i].id] = cur;
      if (i == n - 2 || v[i] != v[i + 1]) cur++;
    }
    k *= 2;
    if (cur == n) break;
  }
  cerr << "after suffarray, time = " << clock() << " ms." << endl;
  vector<pair<int, int> > t;
  for (int i = 0; i < (int)(n - 1); i++) t.push_back(make_pair(p[i], i));
  sort((t).begin(), (t).end());
  for (int i = 0; i < (int)(n - 1); i++) p[i] = t[i].second;
  build(t, 0, 0, OSN);
  cerr << "after build, time = " << clock() << " ms." << endl;
  int m;
  cin >> m;
  for (int q = 0; q < (int)(m); q++) {
    int l, r;
    scanf("%d%d", &l, &r);
    l--;
    r--;
    if (l == r)
      printf("%d\n", n - 1);
    else {
      pair<int, int> pos = findPos(l, r - l);
      int ans = pos.second - pos.first + 1;
      if (r < n - (r - l))
        ans -= get(0, 0, OSN, pos.first, pos.second, n - (r - l), n);
      ans -= get(0, 0, OSN, pos.first, pos.second, 2 * l - r, r);
      printf("%d\n", ans);
    }
  }
  cerr << "after solve, time = " << clock() << " ms." << endl;
  return 0;
}