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 maxn = 200008;
struct Tsuffix {
  int n;
  int rank[maxn], a[maxn], b[maxn], sa[maxn];
  int h[maxn], c[maxn], ts[maxn];
  void make(int s[], int N) {
    int *x = a, *y = b, m, i, p;
    memset(a, 0, sizeof(a));
    memset(b, 0, sizeof(b));
    n = N;
    m = 200000;
    if (m == n) m++;
    for (int i = 1; i <= n; i++) c[x[i] = s[i]]++;
    for (int i = 1; i <= m; i++) c[i] += c[i - 1];
    for (int i = n; i >= 1; i--) sa[c[x[i]]--] = i;
    for (int k = 1; m != n; k <<= 1) {
      p = 0;
      for (int i = n - k + 1; i <= n; i++) y[++p] = i;
      for (int i = 1; i <= n; i++)
        if (sa[i] - k > 0) y[++p] = sa[i] - k;
      for (int i = 1; i <= n; i++) ts[i] = x[y[i]];
      memset(c, 0, m + 1 << 2);
      for (int i = 1; i <= n; i++) c[ts[i]]++;
      for (int i = 1; i <= m; i++) c[i] += c[i - 1];
      for (int i = n; i >= 1; i--) sa[c[ts[i]]--] = y[i];
      for (swap(x, y), i = 2, x[sa[1]] = 1; i <= n; i++)
        if (y[sa[i]] == y[sa[i - 1]] && y[sa[i] + k] == y[sa[i - 1] + k])
          x[sa[i]] = x[sa[i - 1]];
        else
          x[sa[i]] = x[sa[i - 1]] + 1;
      m = x[sa[n]];
    }
    for (int i = 1; i <= n; i++) rank[i] = x[i];
    int k = 0;
    h[1] = n + 1;
    for (int i = 1; i <= n; i++) {
      if (rank[i] == 1) continue;
      while (s[i + k] == s[sa[rank[i] - 1] + k]) k++;
      h[rank[i]] = k;
      k--;
      if (k < 0) k = 0;
    }
  }
} Suffix;
struct Tst {
  int n, f[20][maxn], log[maxn];
  void make(int h[], int N) {
    n = N;
    for (int i = 1; i <= n; i++) f[0][i] = h[i];
    for (int i = 1; i <= 19; i++)
      for (int j = 1; j <= n; j++) {
        if (j + (1 << i) - 1 > n) break;
        f[i][j] = (f[i - 1][j] < f[i - 1][j + (1 << i - 1)]
                       ? f[i - 1][j]
                       : f[i - 1][j + (1 << i - 1)]);
      }
    for (int i = 1; i <= n; i++)
      for (int j = i >> 1; j; j >>= 1) log[i]++;
  }
  int ask(int l, int r) {
    if (l > r) return 0;
    int k = log[r - l + 1];
    return (f[k][l] < f[k][r - (1 << k) + 1] ? f[k][l]
                                             : f[k][r - (1 << k) + 1]);
  }
} St;
int n, b[maxn], c[maxn], ans[maxn];
int flag[maxn];
struct arr {
  int l, r, L, R, i;
} a[maxn];
struct brr {
  int v;
  int *i;
  bool operator<(const brr &t) const { return v < t.v; }
} p[maxn];
bool cmp1(const arr &i, const arr &j) { return 2 * i.l - i.r < 2 * j.l - j.r; }
bool cmp2(const arr &i, const arr &j) { return i.r > j.r; }
struct Tprogram {
  void open() {
    freopen("1.in", "r", stdin);
    freopen("1.out", "w", stdout);
  }
  void close() {}
  void lisan(int b[], int n) {
    int tot = 0;
    for (int i = 1; i <= n; i++) p[++tot].v = b[i], p[tot].i = &b[i];
    sort(p + 1, p + tot + 1);
    int m = 0;
    for (int i = 1; i <= tot; i++) {
      if (p[i].v != p[i - 1].v || i == 1) m++;
      *p[i].i = m;
    }
  }
  void init() {
    scanf("%d", &n);
    for (int i = 1; i <= n; i++) scanf("%d", &b[i]);
    b[0] = 2 * 1e9 + 1;
    for (int i = n; i >= 1; i--) b[i] = b[i] - b[i - 1];
    for (int i = 1; i <= n; i++) b[i + n] = -b[i];
    lisan(b, 2 * n);
    Suffix.make(b, 2 * n);
    St.make(Suffix.h, 2 * n);
  }
  int binary1(int x, int v) {
    int l = 2, r = x;
    if (St.ask(r, r) < v) return x;
    while (l <= r) {
      int m = l + r >> 1;
      if (St.ask(m, r) >= v)
        r = m - 1;
      else
        l = m + 1;
    }
    return r;
  }
  int binary2(int x, int v) {
    int l = x + 1, r = 2 * n;
    if (St.ask(l, l) < v) return x;
    while (l <= r) {
      int m = l + r >> 1;
      if (St.ask(l, m) >= v)
        l = m + 1;
      else
        r = m - 1;
    }
    return r;
  }
  void modify(int x) {
    for (; x <= 2 * n; x += x & -x) c[x]++;
  }
  int ask(int x) {
    int sum = 0;
    for (; x; x -= x & -x) sum += c[x];
    return sum;
  }
  void work() {
    int m;
    scanf("%d", &m);
    for (int i = 1; i <= m; i++) {
      scanf("%d%d", &a[i].l, &a[i].r);
      a[i].l++;
      a[i].i = i;
      if (a[i].l > a[i].r) {
        ans[i] = n - 1;
        flag[i] = 1;
        continue;
      }
      int p = Suffix.rank[a[i].l + n];
      a[i].L = binary1(p, a[i].r - a[i].l + 1);
      a[i].R = binary2(p, a[i].r - a[i].l + 1);
    }
    sort(a + 1, a + m + 1, cmp1);
    int j = 1;
    for (int i = 1; i <= m; i++)
      if (!flag[a[i].i]) {
        while (j <= n && j < 2 * a[i].l - 1 - a[i].r) modify(Suffix.rank[j++]);
        ans[a[i].i] += ask(a[i].R) - ask(a[i].L - 1);
      }
    sort(a + 1, a + m + 1, cmp2);
    j = n;
    memset(c, 0, sizeof(c));
    for (int i = 1; i <= m; i++)
      if (!flag[a[i].i]) {
        while (j && j > a[i].r + 1) modify(Suffix.rank[j--]);
        ans[a[i].i] += ask(a[i].R) - ask(a[i].L - 1);
      }
    for (int i = 1; i <= m; i++) printf("%d\n", ans[i]);
  }
} Program;
int main() {
  Program.init();
  Program.work();
  Program.close();
  return 0;
}