#include <bits/stdc++.h>   using namespace std;   template <typename T>   T gcd(T a, T b) {     return !b ? a : gcd(b, a % b);   }   template <typename T>   T lcm(T a, T b) {     return a * (b / gcd(a, b));   }   template <typename T>   T sqr(T a) {     return a * a;   }   template <typename T>   T cube(T a) {     return a * a * a;   }   template <typename T>   inline void smin(T &a, T b) {     a = a < b ? a : b;   }   template <typename T>   inline void smax(T &a, T b) {     a = a > b ? a : b;   }   int in() {     int n;     scanf( %d , &n);     return n;   }   long long Lin() {     long long n;     scanf( %lld , &n);     return n;   }   double Din() {     double n;     scanf( %lf , &n);     return n;   }   const long long inf = (long long)1e17;   const long long mod = (long long)1e9 + 7;   const int N = 1e5 + 5;   struct CHT_DEC_MIN {     vector<long long> m, b;     bool bad(int f1, int f2, int f3) {       return 1.0 * (b[f2] - b[f1]) * (m[f1] - m[f3]) >=              1.0 * (b[f3] - b[f1]) * (m[f1] - m[f2]);     }     void add(long long M, long long B) {       m.push_back(M), b.push_back(B);       int sz = (int)m.size();       while (sz >= 3 && bad(sz - 3, sz - 2, sz - 1)) {         m.erase(m.end() - 2);         b.erase(b.end() - 2);         sz--;       }     }     long long f(int idx, long long X) { return m[idx] * X + b[idx]; }     long long query(long long X) {       int low = 0, high = (int)m.size() - 1;       while (high - low > 5) {         int mid1 = (low + low + high) / 3;         int mid2 = (low + high + high) / 3;         if (f(mid1, X) >= f(mid2, X)) {           low = mid1 + 1;         } else {           high = mid2 - 1;         }       }       long long res = f(low, X);       for (int i = low + 1; i <= high; i++) {         res = min(res, f(i, X));       }       return res;     }   };   long long a[N], s[N];   CHT_DEC_MIN tree[4 * N];   void update(int pos, int left, int right, int x, long long M, long long B) {     tree[pos].add(M, B);     if (left == right) return;     int mid = (left + right) >> 1;     if (x <= mid)       update(pos * 2, left, mid, x, M, B);     else       update(pos * 2 + 1, mid + 1, right, x, M, B);   }   long long query(int pos, int left, int right, int x, int y, long long X) {     if (left > y || right < x) return (long long)1e17;     if (left >= x && right <= y) {       return tree[pos].query(X);     }     int mid = (left + right) >> 1;     long long m = query(pos * 2, left, mid, x, y, X);     long long n = query(pos * 2 + 1, mid + 1, right, x, y, X);     return min(m, n);   }   int solve() {     int n = in();     vector<pair<long long, int> > tem;     for (int i = 1; i <= n; i++) {       a[i] = Lin();       s[i] += s[i - 1] + a[i];       tem.push_back({a[i], i});     }     sort(tem.begin(), tem.end());     for (int i = 0; i < n; i++) {       int idx = tem[i].second;       long long M = -a[idx], B = a[idx] * idx - s[idx];       update(1, 1, n, idx, M, B);     }     int q = in();     while (q--) {       int x = in(), y = in();       x = y - x + 1;       printf( %lld n , s[y] + query(1, 1, n, x, y, (long long)x - 1));     }     return 0;   }   int main() {     int test = 1, tc = 0;     while (test--) {       solve();     }     return 0;   }