Dreamoon has just created a document of hard problems using notepad.exe. The document consists of n lines of text, ai denotes the length of the i-th line. He now wants to know what is the fastest way to move the cursor around because the document is really long.

Let (r, c) be a current cursor position, where r is row number and c is position of cursor in the row. We have 1 ≤ r ≤ n and 0 ≤ c ≤ ar.

We can use following six operations in notepad.exe to move our cursor assuming the current cursor position is at (r, c):

  1. up key: the new cursor position (nr, nc) = (max(r - 1, 1), min(anr, c))
  2. down key: the new cursor position (nr, nc) = (min(r + 1, n), min(anr, c))
  3. left key: the new cursor position (nr, nc) = (r, max(0, c - 1))
  4. right key: the new cursor position (nr, nc) = (r, min(anr, c + 1))
  5. HOME key: the new cursor position (nr, nc) = (r, 0)
  6. END key: the new cursor position (nr, nc) = (r, ar)



You're given the document description (n and sequence ai) and q queries from Dreamoon. Each query asks what minimal number of key presses is needed to move the cursor from (r1, c1) to (r2, c2).

Input

The first line contains an integer n(1 ≤ n ≤ 400, 000) — the number of lines of text. 

The second line contains n integers a1, a2, ..., an(1 ≤ ai ≤ 108).

The third line contains an integer q(1 ≤ q ≤ 400, 000). 

Each of the next q lines contains four integers r1, c1, r2, c2 representing a query (1 ≤ r1, r2 ≤ n, 0 ≤ c1 ≤ ar1, 0 ≤ c2 ≤ ar2).

Output

For each query print the result of the query.

Examples

Input

9
1 3 5 3 1 3 5 3 1
4
3 5 3 1
3 3 7 3
1 0 3 3
6 0 7 3


Output

2
5
3
2


Input

2
10 5
1
1 0 1 5


Output

3

Note

In the first sample, the first query can be solved with keys: HOME, right.

The second query can be solved with keys: down, down, down, END, down.

The third query can be solved with keys: down, END, down.

The fourth query can be solved with keys: END, down.
#include <bits/stdc++.h>
using namespace std;
const int maxn = 400100;
int a[maxn];
struct Qry {
  int r1, c1, r2, c2, id;
} q[maxn];
int n, Q;
int ST[maxn][20], lg[maxn];
int ans[maxn];
int qrymin(int l, int r) {
  if (l > r) swap(l, r);
  int L = lg[r - l + 1];
  return min(ST[l][L], ST[r - (1 << L) + 1][L]);
}
int st[maxn], tp;
int go(Qry t, int p) {
  int w = min(t.c1, qrymin(st[p], t.r1));
  int cur = abs(st[p] - t.r1) + abs(st[p] - t.r2);
  if (w != a[st[p]]) cur++;
  w = a[st[p]];
  cur += abs(w - t.c2);
  return cur;
}
void solve(Qry c, int l, int r) {
  l--, r++;
  while (l + 8 < r) {
    int lm = (l + l + r) / 3, rm = (l + r + r) / 3;
    if (go(c, lm) > go(c, rm))
      l = lm;
    else
      r = rm;
  }
  for (int i = l + 1; i <= r - 1; i++) ans[c.id] = min(ans[c.id], go(c, i));
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0), cout.tie(0);
  cin >> n;
  lg[0] = -1;
  for (int i = 1; i <= n; i++)
    cin >> a[i], ST[i][0] = a[i], lg[i] = lg[i / 2] + 1;
  for (int j = 1; j < 20; j++) {
    for (int i = 1; i + (1 << j) - 1 <= n; i++)
      ST[i][j] = min(ST[i][j - 1], ST[i + (1 << j - 1)][j - 1]);
  }
  cin >> Q;
  for (int i = 1; i <= Q; i++) {
    cin >> q[i].r1 >> q[i].c1 >> q[i].r2 >> q[i].c2;
    q[i].id = i;
    ans[i] = abs(q[i].r2 - q[i].r1) + 1 + q[i].c2;
    ans[i] = min(ans[i], abs(min(q[i].c1, qrymin(q[i].r1, q[i].r2)) - q[i].c2) +
                             abs(q[i].r2 - q[i].r1));
  }
  sort(q + 1, q + Q + 1, [&](Qry a, Qry b) { return a.r2 < b.r2; });
  tp = 0;
  int j = 1, p;
  for (int i = 1; i <= n; i++) {
    while (tp && a[st[tp]] >= a[i]) tp--;
    st[++tp] = i;
    while (j != Q + 1 && q[j].r2 == i) {
      if (q[j].r1 <= q[j].r2) {
        p = lower_bound(st + 1, st + tp + 1, q[j].r1) - st;
        solve(q[j], 1, p - 1);
        solve(q[j], p, tp);
      } else {
        solve(q[j], 1, tp);
      }
      j++;
    }
  }
  tp = 0, j = Q;
  for (int i = n; i; i--) {
    while (tp && a[st[tp]] >= a[i]) tp--;
    st[++tp] = i;
    while (j != 0 && q[j].r2 == i) {
      if (q[j].r1 > q[j].r2) {
        int L = 1, R = tp;
        while (L != R) {
          int mid = L + R >> 1;
          if (st[mid] <= q[j].r1)
            R = mid;
          else
            L = mid + 1;
        }
        p = L;
        solve(q[j], 1, p - 1);
        solve(q[j], p, tp);
      } else {
        solve(q[j], 1, tp);
      }
      j--;
    }
  }
  for (int i = 1; i <= Q; i++) {
    cout << ans[i] << "\n";
  }
  return 0;
}
