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;
inline void pisz(int n) { printf("%d\n", n); }
template <typename T, typename TT>
ostream &operator<<(ostream &s, pair<T, TT> t) {
  return s << "(" << t.first << "," << t.second << ")";
}
template <typename T>
ostream &operator<<(ostream &s, vector<T> t) {
  for (int(i) = 0; (i) < (((int)((t).size()))); ++(i)) s << t[i] << " ";
  return s;
}
int *g, h;
bool comp1(int i, int j) { return g[i] < g[j]; }
inline bool comp(int i, int j) {
  if (i == j) return 0;
  if (g[i] != g[j]) return g[i] < g[j];
  return g[i + h] < g[j + h];
}
struct SuffixArray {
  int *sa, *lcp, *revsa;
  SuffixArray(int *str, bool with_lcp, int n) {
    sa = new int[n + 3];
    g = new int[n + 1];
    int *b = new int[n + 1];
    for (int(i) = 0; (i) < (n + 1); ++(i)) {
      sa[i] = i;
      g[i] = i == n ? -1 : str[i];
    }
    b[0] = b[n] = 0;
    sort(sa, sa + n + 1, comp1);
    for (h = 1; b[n] != n; h *= 2) {
      sort(sa, sa + n + 1, comp);
      for (int(i) = 0; (i) < (n); ++(i))
        b[i + 1] = b[i] + (sa[i] == sa[i + 1] || comp(sa[i], sa[i + 1]));
      for (int(i) = 0; (i) < (n + 1); ++(i)) g[sa[i]] = b[i];
    }
    for (int(i) = 0; (i) < (n); ++(i)) sa[i] = sa[i + 1];
    delete[] b;
    delete[] g;
    if (with_lcp) {
      lcp = new int[n + 3];
      vector<int> rank(n + 3);
      for (int(i) = 0; (i) < (n); ++(i)) rank[sa[i]] = i;
      for (int i = 0, k = 0; i < n; i++) {
        if (k > 0) k--;
        if (rank[i] == n - 1) {
          lcp[n - 1] = -1;
          k = 0;
          continue;
        }
        int j = sa[rank[i] + 1];
        while (str[i + k] == str[j + k]) k++;
        lcp[rank[i]] = k;
      }
    } else {
      lcp = NULL;
    }
    revsa = new int[n + 3];
    for (int(i) = 0; (i) < (n); ++(i)) revsa[sa[i]] = i;
  }
  ~SuffixArray() {
    delete[] sa;
    if (lcp != NULL) delete[] lcp;
  }
};
template <typename T, typename Aug>
struct AugTreap {
  struct Node {
    T v;
    int pr;
    Node *le, *ri;
    Aug al, ag;
  };
  Node *root;
  AugTreap() : root(0) {}
  inline static void recalc(Node *&n) {
    Aug left = n->le ? n->le->ag + n->al : n->al;
    n->ag = n->ri ? left + n->ri->ag : left;
  }
  inline void rotle(Node *&n) {
    Node *syn = n->le;
    n->le = syn->ri;
    syn->ri = n;
    n = syn;
    recalc(n->ri);
    recalc(n);
  }
  inline void rotri(Node *&n) {
    Node *syn = n->ri;
    n->ri = syn->le;
    syn->le = n;
    n = syn;
    recalc(n->le);
    recalc(n);
  }
  void insert_aux(Node *&n, const T &x, const Aug &aug) {
    if (!n) {
      n = new Node;
      n->v = x;
      n->pr = rand();
      n->le = n->ri = 0;
      n->al = n->ag = aug;
    } else if (x < n->v) {
      insert_aux(n->le, x, aug);
      if (n->pr > n->le->pr)
        rotle(n);
      else
        recalc(n);
    } else if (x > n->v) {
      insert_aux(n->ri, x, aug);
      if (n->pr > n->ri->pr)
        rotri(n);
      else
        recalc(n);
    } else {
      n->al = n->al + aug;
      recalc(n);
    }
  }
  void insert(const T &x, const Aug &aug) { insert_aux(root, x, aug); }
  Aug sumgeq_aux(const T &x, Node *n) {
    if (!n) return Aug();
    if (x > n->v) return sumgeq_aux(x, n->ri);
    if (x < n->v) {
      Aug left = sumgeq_aux(x, n->le) + n->al;
      return n->ri ? left + n->ri->ag : left;
    }
    return n->ri ? n->al + n->ri->ag : n->al;
  }
  Aug sumgeq(const T &x) { return sumgeq_aux(x, root); }
  Aug sumleq_aux(const T &x, Node *n) {
    if (!n) return Aug();
    if (x < n->v) return sumleq_aux(x, n->le);
    if (x > n->v) {
      Aug right = n->al + sumleq_aux(x, n->ri);
      return n->le ? n->le->ag + right : right;
    }
    return n->le ? n->le->ag + n->al : n->al;
  }
  Aug sumleq(const T &x) { return sumleq_aux(x, root); }
  Aug sumbetween_aux(const T &x, const T &y, Node *n) {
    if (!n) return Aug();
    if (y < n->v) return sumbetween_aux(x, y, n->le);
    if (x > n->v) return sumbetween_aux(x, y, n->ri);
    return sumgeq_aux(x, n->le) + n->al + sumleq_aux(y, n->ri);
  }
  Aug sumbetween(const T &x, const T &y) { return sumbetween_aux(x, y, root); }
};
struct IntMin {
  int x;
  IntMin(int y = 1001001001) : x(y) {}
  inline IntMin operator+(const IntMin &s) const { return IntMin(min(x, s.x)); }
  operator int() const { return x; }
};
int inp[200009];
int word[200009];
int ans[200009];
int newword[200009];
struct Todo {
  int from, to, maxIndex, whichQuery, multiplier, uniq;
};
bool operator<(const Todo &t, const Todo &r) {
  return t.maxIndex != r.maxIndex ? t.maxIndex < r.maxIndex : t.uniq < r.uniq;
}
int main() {
  int(n);
  scanf("%d", &(n));
  for (int(i) = 0; (i) < (n); ++(i)) {
    scanf("%d", inp + i);
  }
  for (int(i) = 0; (i) < (n - 1); ++(i)) {
    word[i] = inp[i + 1] - inp[i];
  }
  word[n - 1] = INT_MAX;
  for (int(i) = 0; (i) < (n - 1); ++(i)) {
    word[n + i] = -word[i];
  }
  int len = 2 * n - 1;
  map<int, int> mapa;
  int now = 1;
  for (int(i) = 0; (i) < (len); ++(i)) mapa[word[i]] = now++;
  for (int(i) = 0; (i) < (len); ++(i)) newword[i] = mapa[word[i]];
  SuffixArray sa(newword, 1, len);
  AugTreap<int, IntMin> lcpTreap;
  for (int(i) = 0; (i) < (len - 1); ++(i)) lcpTreap.insert(i, sa.lcp[i]);
  vector<Todo> todo;
  int uniquer = 0;
  int(queries);
  scanf("%d", &(queries));
  for (int(query) = 0; (query) < (queries); ++(query)) {
    int(l), (r);
    scanf("%d %d", &(l), &(r));
    --l;
    --r;
    if (l == r) {
      ans[query] = n - 1;
    } else {
      ans[query] = 0;
      int indexInSa = sa.revsa[n + l];
      int a = indexInSa, b = indexInSa;
      {
        int from = 0, to = indexInSa - 1;
        while (from <= to) {
          int mid = (from + to) / 2;
          int lcp = lcpTreap.sumbetween(mid, indexInSa - 1);
          if (lcp >= r - l) {
            a = mid;
            to = mid - 1;
          } else {
            from = mid + 1;
          }
        }
      }
      {
        int from = indexInSa + 1, to = len - 1;
        while (from <= to) {
          int mid = (from + to) / 2;
          int lcp = lcpTreap.sumbetween(indexInSa, mid - 1);
          if (lcp >= r - l) {
            b = mid;
            from = mid + 1;
          } else {
            to = mid - 1;
          }
        }
      }
      if (r + 1 <= n - 2) {
        todo.push_back((Todo){a, b, n - 2, query, 1, uniquer++});
        todo.push_back((Todo){a, b, r, query, -1, uniquer++});
      }
      if (0 <= l - (r - l + 1)) {
        todo.push_back((Todo){a, b, l - (r - l + 1), query, 1, uniquer++});
        todo.push_back((Todo){a, b, -1, query, -1, uniquer++});
      }
    }
  }
  sort(todo.begin(), todo.end());
  int nowToAddIndex = 0;
  AugTreap<int, int> cntTreap;
  for (typeof(todo.begin()) td = todo.begin(); td != todo.end(); td++) {
    for (; nowToAddIndex <= td->maxIndex; ++nowToAddIndex) {
      cntTreap.insert(sa.revsa[nowToAddIndex], 1);
    }
    int res = cntTreap.sumbetween(td->from, td->to);
    ans[td->whichQuery] += td->multiplier * res;
  }
  for (int(query) = 0; (query) < (queries); ++(query)) pisz(ans[query]);
}