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>
T abs(T x) {
  return x > 0 ? x : -x;
}
struct node {
  node *l, *r;
  int size;
  int key;
  int pr;
  node() {
    l = r = NULL;
    size = 1;
    pr = rand();
  }
  void update() {
    size = 1;
    if (l) size += l->size;
    if (r) size += r->size;
  }
};
typedef node *pnode;
int getSize(pnode v) { return v ? v->size : 0; }
pnode merge(pnode l, pnode r) {
  if (!l || !r) return l ? l : r;
  pnode res;
  if (l->pr < r->pr) {
    res = l;
    res->r = merge(res->r, r);
  } else {
    res = r;
    res->l = merge(l, res->l);
  }
  res->update();
  return res;
}
void split(pnode v, pnode &l, pnode &r, long long key) {
  if (!v) {
    l = r = NULL;
    return;
  }
  if (v->key <= key) {
    l = v;
    split(l->r, l->r, r, key);
  } else {
    r = v;
    split(r->l, l, r->l, key);
  }
  if (l) l->update();
  if (r) r->update();
}
pnode insert(pnode v, long long key) {
  pnode _left, center, right;
  split(v, _left, center, key - 1);
  split(center, center, right, key);
  if (center) {
    return merge(_left, merge(center, right));
  } else {
    center = new node();
    center->key = key;
    return merge(merge(_left, center), right);
  }
}
pnode mergeSets(pnode v, pnode acc) {
  if (!v) return acc;
  acc = insert(acc, v->key);
  acc = mergeSets(v->l, acc);
  acc = mergeSets(v->r, acc);
  return acc;
}
pair<int, int> queries[100005];
int ans[100005];
struct sa {
  unordered_map<int, sa *> nxt;
  sa *lnk;
  int len;
  pnode pos;
  sa *lnkP[19];
  bool visited;
  vector<sa *> g;
  vector<int> q;
  sa() {
    lnk = NULL;
    len = 0;
    pos = NULL;
    visited = false;
    for (int i = 0; i < 19; i++) lnkP[i] = NULL;
  }
};
sa *root = new sa();
sa *last = root;
sa *pool[400505];
int poolCnt = 0;
sa *byLen[400505];
void extend(long long x, bool f) {
  sa *curr = new sa();
  pool[poolCnt++] = curr;
  last->nxt[x] = curr;
  curr->lnk = root;
  curr->len = last->len + 1;
  byLen[curr->len] = curr;
  if (f) curr->pos = insert(NULL, curr->len);
  sa *v = last->lnk;
  while (v != NULL) {
    if (!v->nxt.count(x)) {
      v->nxt[x] = curr;
      v = v->lnk;
    } else {
      if (v->nxt[x]->len == v->len + 1) {
        curr->lnk = v->nxt[x];
      } else {
        sa *p = v, *q = p->nxt[x];
        sa *clone = new sa();
        pool[poolCnt++] = clone;
        clone->lnk = q->lnk;
        clone->nxt = q->nxt;
        clone->len = p->len + 1;
        q->lnk = clone;
        curr->lnk = clone;
        for (; p && p->nxt.count(x) && p->nxt[x] == q; p = p->lnk)
          p->nxt[x] = clone;
      }
      break;
    }
  }
  last = curr;
}
const int maxn = 100005;
long long a[maxn];
void printSet(node *v) {
  if (!v) return;
  cerr << " " << v->key << endl;
  printSet(v->l);
  printSet(v->r);
}
void compLinks(sa *v) {
  if (v->visited) return;
  v->visited = true;
  if (v->lnk) compLinks(v->lnk);
  v->lnkP[0] = v->lnk;
  for (int i = 1; i < 17; i++) {
    if (v->lnkP[i - 1]) v->lnkP[i] = v->lnkP[i - 1]->lnkP[i - 1];
  }
}
pnode dfs(sa *v) {
  for (auto to : v->g) {
    pnode res = dfs(to);
    if (getSize(v->pos) < getSize(res))
      v->pos = mergeSets(v->pos, res);
    else
      v->pos = mergeSets(res, v->pos);
  }
  for (int x : v->q) {
    int l = queries[x].first;
    int r = queries[x].second;
    pnode _left, center, right;
    split(v->pos, _left, center, l - 1);
    split(center, center, right, r + r - l);
    ans[x] = getSize(_left) + getSize(right);
    v->pos = merge(merge(_left, center), right);
  }
  return v->pos;
}
int main(int argc, char *argv[]) {
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) cin >> a[i];
  pool[poolCnt++] = root;
  for (int i = 1; i < n; i++) extend(a[i] - a[i - 1], true);
  extend(1e18, false);
  for (int i = 1; i < n; i++) extend(a[i - 1] - a[i], false);
  for (int i = 0; i < poolCnt; i++) {
    if (pool[i]->lnk) pool[i]->lnk->g.push_back(pool[i]);
    compLinks(pool[i]);
  }
  int m;
  cin >> m;
  for (int i = 0; i < m; i++) {
    int l, r;
    cin >> l >> r;
    l--;
    r--;
    queries[i] = make_pair(l, r);
    if (l == r) {
      ans[i] = n - 1;
      continue;
    }
    sa *v = byLen[n + r];
    for (int j = 18; j >= 0; j--) {
      if (v->lnkP[j] && v->lnkP[j]->len >= r - l) v = v->lnkP[j];
    }
    v->q.push_back(i);
  }
  for (int i = 0; i < poolCnt; i++)
    if (!pool[i]->lnk) dfs(pool[i]);
  for (int i = 0; i < m; i++) printf("%d\n", ans[i]);
  return 0;
}