#include <bits/stdc++.h>   using namespace std;   const int maxn = 400005;   int n, q, a[maxn];   namespace SGT {   int Min[maxn << 2];   void modify(int l, int r, int id, int x, int y) {     if (l == r) {       Min[id] = y;       return;     }     int mid = (l + r) >> 1;     if (x <= mid)       modify(l, mid, id << 1, x, y);     else       modify(mid + 1, r, id << 1 | 1, x, y);     Min[id] = min(Min[id << 1], Min[id << 1 | 1]);   }   int query(int l, int r, int id, int L, int R) {     if (L <= l && r <= R) return Min[id];     int mid = (l + r) >> 1, res = 1e9;     if (L <= mid) res = min(res, query(l, mid, id << 1, L, R));     if (R > mid) res = min(res, query(mid + 1, r, id << 1 | 1, L, R));     return res;   }   }  // namespace SGT   struct query {     int r1, c1, c2, id;     query() {}     query(int _r1, int _c1, int _c2, int _i) {       r1 = _r1, c1 = _c1, c2 = _c2, id = _i;     }   };   int ans[maxn], now[maxn];   vector<query> Q[2][maxn];   void solve(vector<query> *Q) {     static int st[maxn], num[maxn], top;     top = 0;     for (int i = 1; i <= n; i++) {       while (top && num[top] >= a[i]) top--;       st[++top] = i;       num[top] = a[i];       SGT ::modify(1, n, 1, top, -2 * i + a[i]);       for (auto u : Q[i]) {         int r1 = u.r1, c1 = u.c1, r2 = i, c2 = u.c2, id = u.id;         int res = r2 - r1 + c2 + 1;         int L = lower_bound(st + 1, st + top + 1, r1) - st, R = top;         now[id] = min(c1, num[L]);         res = min(res, r2 - r1 + abs(c2 - now[id]));         int pos = lower_bound(num + L, num + R + 1, c2) - num;         if (pos <= R) res = min(res, r2 - r1 + 1 + abs(c2 - num[pos]));         if (pos > L)           res = min(res, r2 - r1 + 1 + abs(c2 - num[pos - 1]));         else {           assert(pos == L);           pos = upper_bound(num + 1, num + R + 1, c2) - num - 1;           if (pos && st[pos] <= r1)             res = min(res, r1 + r2 - 2 * st[pos] + (c1 < num[pos]) +                                abs(c2 - num[pos]));         }         if (pos + 1 < L) {           R = L - 1, L = pos + 1;           int mid = upper_bound(num + L, num + R + 1, c1) - num;           if (mid <= R)             res = min(res, r1 + r2 + 1 + SGT ::query(1, n, 1, mid, R) - c2);           if (L < mid)             res = min(res, r1 + r2 + SGT ::query(1, n, 1, L, mid - 1) - c2);         }         ans[id] = res;       }     }     top = 0;     for (int i = n; i >= 1; i--) {       while (top && num[top] >= a[i]) top--;       st[++top] = i;       num[top] = a[i];       SGT ::modify(1, n, 1, top, 2 * i + a[i]);       for (auto u : Q[i]) {         int r1 = u.r1, r2 = i, c2 = u.c2, id = u.id;         int pos = upper_bound(num + 1, num + top + 1, c2) - num - 1, res = 1e9;         if (pos)           res = -(r1 + r2) + 2 * st[pos] + (now[id] < num[pos]) +                 abs(c2 - num[pos]);         if (pos + 1 <= top) {           int L = pos + 1, R = top;           int mid = upper_bound(num + L, num + R + 1, now[id]) - num;           if (mid <= R)             res = min(res, -(r1 + r2) + 1 + SGT ::query(1, n, 1, mid, R) - c2);           if (L < mid)             res = min(res, -(r1 + r2) + SGT ::query(1, n, 1, L, mid - 1) - c2);         }         ans[id] = min(ans[id], res);       }     }   }   int main() {     scanf( %d , &n);     for (int i = 1; i <= n; i++) scanf( %d , &a[i]);     scanf( %d , &q);     for (int i = 1, r1, c1, r2, c2; i <= q; i++) {       scanf( %d%d%d%d , &r1, &c1, &r2, &c2);       if (r1 <= r2)         Q[0][r2].push_back(query(r1, c1, c2, i));       else         Q[1][n - r2 + 1].push_back(query(n - r1 + 1, c1, c2, i));     }     solve(Q[0]);     reverse(a + 1, a + n + 1);     solve(Q[1]);     for (int i = 1; i <= q; i++) printf( %d n , ans[i]);     return 0;   }