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 int N = 400123;
const int inf = 1e9 + 3;
int z, n, q, a, b;
string s;
int ans[N];
struct Query {
  int a, b, id;
};
struct SuffixAutomaton {
  struct State {
    map<int, int> nbs;
    int suf = -1, len = 0, minlen = 0, vis = 0, first = 0;
    vector<int> ways;
    bool good = false;
    bool clone = false;
    vector<Query> qs;
  };
  struct fenwick {
    int ft[N];
    fenwick() {}
    void clear() { memset(ft, 0, sizeof ft); }
    void insert(int second, int v) {
      second++;
      for (; second < N; second += second & -second) ft[second] += v;
    }
    int q(int second) {
      int res = 0;
      for (; second; second -= second & -second) res += ft[second];
      return res;
    }
    int query(int second, int first) {
      if (second > first) return 0;
      if (first >= N) return 0;
      second++;
      first++;
      return q(first) - (second > 1 ? q(second - 1) : 0);
    }
  };
  fenwick tree;
  vector<State> first;
  int up[20][N];
  int wherefirst[N];
  vector<int> g[N];
  void lca() {
    for (int i = 0; i < first.size(); i++) up[0][i] = max(0, first[i].suf);
    for (int d = 1; d < 20; d++) {
      for (int i = 0; i < first.size(); i++) {
        up[d][i] = up[d - 1][up[d - 1][i]];
      }
    }
  }
  int bs(int s, int len) {
    for (int d = 19; d >= 0; d--) {
      if (len <= first[up[d][s]].len) {
        s = up[d][s];
      }
    }
    return s;
  }
  int get_query_vertex(int a, int b) {
    a++;
    a += n;
    b += n;
    if (a > b) return n - 1;
    int len = b - a + 1;
    return bs(wherefirst[b], len);
  }
  void init(vector<int>& s) {
    int last = 0;
    first.reserve(2 * s.size());
    first.push_back(State());
    for (int i = 0; i < s.size(); i++) {
      int c = s[i];
      first.push_back(State());
      int r = first.size() - 1;
      first[r].suf = 0;
      first[r].len = i + 1;
      first[r].first = i;
      wherefirst[i] = r;
      int p = last;
      while (p >= 0 && !first[p].nbs.count(c)) {
        first[p].nbs[c] = r;
        p = first[p].suf;
      }
      if (p != -1) {
        int q = first[p].nbs[c];
        if (first[p].len + 1 == first[q].len) {
          first[r].suf = q;
        } else {
          first.push_back(first[q]);
          int qq = first.size() - 1;
          first[qq].len = first[p].len + 1;
          first[qq].clone = true;
          first[q].suf = first[r].suf = qq;
          while (p >= 0 && first[p].nbs[c] == q) {
            first[p].nbs[c] = qq;
            p = first[p].suf;
          }
        }
      }
      first[r].minlen = first[first[r].suf].len + 1;
      last = r;
      if (i == n - 1) {
        for (State& s : first) s.good = true;
      }
    }
  }
  void build() {
    for (int v = 0; v < first.size(); v++) {
      if (v) {
        g[first[v].suf].push_back(v);
      }
    }
  }
  void work(int v) {
    for (Query q : first[v].qs) {
      int cur = 0;
      cur -= tree.query(q.b + (q.b - q.a + 1), N - 2);
      cur -= tree.query(1, q.a - 1);
      ans[q.id] = cur;
    }
    if (first[v].good && !first[v].clone) {
      tree.insert(first[v].first, 1);
    }
    for (int w : g[v]) {
      work(w);
    }
    for (Query q : first[v].qs) {
      int cur = 0;
      cur += tree.query(q.b + (q.b - q.a + 1), N - 2);
      cur += tree.query(1, q.a - 1);
      ans[q.id] += cur;
    }
  }
};
SuffixAutomaton sa;
vector<int> v;
int main() {
  ios_base::sync_with_stdio(0);
  cin >> n;
  int last;
  for (int i = 0; i < n; i++) {
    int t;
    cin >> t;
    if (!i)
      v.push_back(inf);
    else
      v.push_back(t - last);
    last = t;
  }
  for (int i = 0; i < n; i++) {
    v.push_back(-v[i]);
  }
  sa.init(v);
  sa.build();
  sa.lca();
  cin >> q;
  for (int i = 0; i < q; i++) {
    cin >> a >> b;
    --a;
    --b;
    if (a >= b) {
      ans[i] = n - 1;
      continue;
    }
    int v = sa.get_query_vertex(a, b);
    sa.first[v].qs.push_back({a, b, i});
  }
  sa.work(0);
  for (int i = 0; i < q; i++) {
    cout << ans[i] << "\n";
  }
  return 0;
}