#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef vector<int> vi;
typedef pair<int, int> pi;
typedef vector<ll> vl;
typedef long double ld;
#define txt                        \
    freopen("ip.txt", "r", stdin); \
    freopen("op.txt", "w", stdout)
#define a3 INT_MAX
#define i3 INT_MIN
#define a6 INT64_MAX
#define i6 INT64_MIN
#define F first
#define S second
#define pb push_back
#define sz(v) (int)v.size()
#define fo(i, n) for (int i = 0; i < (int)n; i++)
#define rep(i, k, n) for (int i = k < (int)n ? k : k - 1; k < (int)n ? i < (int)n : i >= (int)n; k < (int)n ? i += 1 : i -= 1)
#define quickie                       \
    ios_base::sync_with_stdio(false); \
    cin.tie(NULL);
#define deb(x) cout << (#x) << " " << (x) << "\n";
#define newl cout << "\n"
#define all(s) s.begin(), s.end()
#define pi 3.14159265358979323846
#define fp(x) fixed << setprecision(x) <<
#define ub(a, v) upper_bound(a.begin(), arr.end(), v)
#define lb(a, v) lower_bound(a.begin(), arr.end(), v)
#define w(t) while (t--)
#define ee in()
#define er ien()
int in()
{
    int res;
    cin >> res;
    return res;
}
ll ien()
{
    ll res;
    cin >> res;
    return res;
}
template <class T>
bool div2(T a)
{
    return !(a & 1);
}
ll mod(ll a)
{
    if (a < 0)
    {
        a += 1000000007;
    }
    return a % 1000000007;
}
ll addmod(ll a, ll b)
{
    return (mod(a) + mod(b)) % 1000000007;
}
ll prodmod(ll a, ll b)
{
    return (mod(a) * mod(b)) % 1000000007;
}
ll e(ll a, ll b)
{
    ll res = 1;
    ll temp = 1;
    if (b == 1)
        return a;
    if (b == 0)
        return 1;
    if (b % 2 == 0)
        res = e(a * a, b / 2);
    else
    {
        res *= a;
        temp = e(a, --b);
    }
    return res * temp;
}
ll emod(ll a, ll b)
{
    ll res = 1;
    ll temp = 1;
    if (b == 1)
        return a;
    if (b == 0)
        return 1;
    if (b % 2 == 0)
        res = emod(prodmod(a, a), b / 2);
    else
    {
        res = prodmod(a, res);
        temp = emod(a, --b);
    }
    return prodmod(res, temp);
}

template <class T>
void peakValley(vector<T> &arr)
{
    //int n;
    //cin>>n
    // vi arr(n+1);
    // fo(i,n){
    //     cin>>arr[i];
    // }
    // arr[n] = INT_MIN;
    // bool b = 1;
    // cout<<"local maxima :";
    // rep(i,0,n){
    //     if(arr[i]>arr[i+1]&&b){
    //         //local maxima
    //         b=0;
    //     }
    //     if(arr[i]<arr[i+1]&&!b){
    //         b=1;
    //         cout<<arr[i]<<" ";
    //         // local minim
    //     }
    // }
    int n = sz(arr);
    arr.resize(n + 2);
    rotate(arr.begin(), arr.begin() + n + 1, arr.end());
    arr[0] = arr[1];

    bool sign = 1;
    ll sum = 0;
    T lastValley = -1;
    T lastPeak = -1;
    T firstValley = -1;
    T firstPeak = -1;
    bool fp = 1;
    bool fv = 1;
    cout << "all valleys ";
    for (int i = 1; i < n + 2 - 1; i++)
    {
        //first peak;
        if (arr[i - 1] <= arr[i] and arr[i] >= arr[i + 1])
        {
            //i is the first peak
            if (fp)
            {
                firstPeak = arr[i];
                fp = 0;
            }
            sign = 0;
            sum += arr[i];
            lastPeak = arr[i];
        }
        if (arr[i - 1] >= arr[i] and arr[i] <= arr[i + 1])
        {
            //valley
            if (fv)
            {
                firstValley = arr[i];
                fv = 0;
            }
            cout << arr[i] << " ";
            sign = 1;
            sum -= arr[i];
            lastValley = arr[i];
        }
    }
    //if(sign) ->what is last visited sign = 0 lastVisited is a peak
    //if(firstPeak||firstValley||lastPeak||lastValley != -1) ->:
    //gives the first and last visited peaks and valleys
}
template <class T>
void findLocalMaximaMinima(vector<T> arr)
{
    int n = arr.size();
    vector<int> mx, mn;

    if (arr[0] > arr[1])
        mx.push_back(0);

    else if (arr[0] < arr[1])
        mn.push_back(0);

    for (int i = 1; i < n - 1; i++)
    {

        if ((arr[i - 1] > arr[i]) and
            (arr[i] < arr[i + 1]))
            mn.push_back(i);

        else if ((arr[i - 1] < arr[i]) and
                 (arr[i] > arr[i + 1]))
            mx.push_back(i);
    }
    if (arr[n - 1] > arr[n - 2])
        mx.push_back(n - 1);

    else if (arr[n - 1] < arr[n - 2])
        mn.push_back(n - 1);
}
template <class T>
bool ispeak(int i, vector<T> a)
{
    int n = a.size();
    if (i < 0 || i >= n)
        return 0;
    return ((i == 0 || a[i - 1] < a[i]) && ((i == n - 1) || a[i] > a[i + 1]));
}
template <class T>
bool islow(int i, vector<T> a)
{
    int n = a.size();
    if (i < 0 || i >= n)
        return 0;
    return ((i > 0 && i < n - 1 && a[i - 1] > a[i] && a[i + 1] > a[i]));
}
template <class T>
int digits(T a)
{
    vector<long long> dig = {
        0,
        9,
        99,
        999,
        9999,
        99999,
        999999,
        9999999,
        99999999,
        999999999,
        9999999999,
        99999999999,
        999999999999,
        9999999999999,
        99999999999999,
        999999999999999,
        9999999999999999,
        99999999999999999,
        999999999999999999,
        INT64_MAX};
    auto it = lower_bound(dig.begin(), dig.end(), a);
    int idx = (it - dig.begin());
    return idx;
}
template <class T>
int sumint(T n)
{
    int a = 1;
    int su = 0;
    while (n)
    {
        su += n % 10;
        n /= 10;
    }
    return su;
}
template <class T>
void split(char c, T s)
{
    int n = s.size();
    int i = 0;
    while ((cout << s[i], i += 1) && (i < n) && (cout << c << ' '))
        ;
}
template <class T>
ll fib(T a)
{
    return ((a * (a + 1)) / 2);
}

template <class T>
ll nC2(T a)
{
    return ((a * (a - 1)) / 2);
}

template <class T>
vector<T> &divisors(T n)
{
    vector<T> *res = new vector<T>;
    vector<T> &ret = *res;
    int R = sqrt(n);
    ret.resize(R);
    int start = 0;
    int end = R - 1;
    for (int i = 2; i <= R; i++)
    {
        if (n % i == 0)
        {
            ret[start] = i;
            start++;
            T etc = n / i;
            if (etc != i)
            {
                ret[end] = etc;
                end--;
            }
        }
    }
    int s = INT_MAX;
    int en = -1;
    for (int i = 0; i < R; i++)
    {
        if (!ret[i])
        {
            s = min(s, i);
            en = i;
        }
    }
    if (en - s >= 0)
        ret.erase(ret.begin() + s, ret.begin() + en + 1);
    return ret;
}
template <class T>
void pvec(vector<T> &vec)
{
    for (auto c : vec)
        cout << c << " ";
}
ll strsum(string s)
{
    ll ret = 0;
    rep(i, sz(s), 0)
    {
        ret += s[i] - '0';
    }
    return ret;
}
bool isPalindrome(string str)
{
    int l = 0;
    int h = sz(str) - 1;

    while (h > l)
    {
        if (str[l++] != str[h--])
        {

            return 0;
        }
    }
    return 1;
}
string addstr(string a, string b)
{
    ll m = sz(a);
    ll n = sz(b);
    ll z = max(n, m);
    string c = string(z, '0');
    bool carry = 0;
    while (n > 0 || m > 0)
    {
        int t = ((m - 1 >= 0) ? (a[m - 1] - '0') : 0) + ((n - 1 >= 0) ? (b[n - 1] - '0') : 0) + carry;
        carry = (t > 9) ? 1 : 0;
        z--;
        c[z] = '0' + (t % 10);
        n--;
        m--;
    }
    if (carry)
        c = "1" + c;
    return c;
}
template <class T>
bool ith(T a, int b)
{
    return ((a & ((ll)1 << (b - 1))));
}
//fxns : div2, mod, addmod, prodmod, bin_expmod, bin_exp as e()
// digits, sumint, split, fib, nC2, divisors,
// pvec, isPalindrome, strsum, addstr,  ith
template<class T>
vector<ll>& presum(vector<T> a)
{
    vector<ll>* temp = new vector<ll>(sz(a));
    vector<ll> &ret = *temp;
    ll sum = 0;
    fo(i, sz(a))
    {
        sum += a[i];
        ret[i] = sum;
    }
    return ret;
}

void solve(int &test)
{
    int n;
    cin >> n;
    vi arr(n);
    ll sum = 0;
    fo(i, n) cin >> arr[i], sum += arr[i];
    sort(all(arr));
    vector<ll> arrr = presum(arr);
    // pvec(arrr);
    int m;
    cin >> m;
    int idx = 0;
    int maxn = a3;
    bool f = 0;
    for (int i = 0; i < m && idx < n; i++)
    {
        int a;
        cin >> a;
        int b;
        cin >> b;
        if (b >= sum && !f)
        {
            maxn = max(sum, (ll)a);
            f = 1;
        }
    }
    if (maxn == a3)
    {
        cout << -1;
    }
    else
    {
        cout << maxn;
    }
}
int main()
{
    quickie;
    //txt;
#ifdef amit
    freopen("ip.txt", "r", stdin);
#endif

    int test;
    test = 0;
    if (test)
        cin >> test;
    else
        test = 1;
    while (test--)
    {
        solve(test);
        newl;
    }
    //cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s."<<endl;
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef vector<int> vi;
typedef pair<int, int> pi;
typedef vector<ll> vl;
typedef long double ld;
#define txt                        \
    freopen("ip.txt", "r", stdin); \
    freopen("op.txt", "w", stdout)
#define a3 INT_MAX
#define i3 INT_MIN
#define a6 INT64_MAX
#define i6 INT64_MIN
#define F first
#define S second
#define pb push_back
#define sz(v) (int)v.size()
#define fo(i, n) for (int i = 0; i < (int)n; i++)
#define rep(i, k, n) for (int i = k < (int)n ? k : k - 1; k < (int)n ? i < (int)n : i >= (int)n; k < (int)n ? i += 1 : i -= 1)
#define quickie                       \
    ios_base::sync_with_stdio(false); \
    cin.tie(NULL);
#define deb(x) cout << (#x) << " " << (x) << "\n";
#define newl cout << "\n"
#define all(s) s.begin(), s.end()
#define pi 3.14159265358979323846
#define fp(x) fixed << setprecision(x) <<
#define ub(a, v) upper_bound(a.begin(), arr.end(), v)
#define lb(a, v) lower_bound(a.begin(), arr.end(), v)
#define w(t) while (t--)
#define ee in()
#define er ien()
int in()
{
    int res;
    cin >> res;
    return res;
}
ll ien()
{
    ll res;
    cin >> res;
    return res;
}
template <class T>
bool div2(T a)
{
    return !(a & 1);
}
ll mod(ll a)
{
    if (a < 0)
    {
        a += 1000000007;
    }
    return a % 1000000007;
}
ll addmod(ll a, ll b)
{
    return (mod(a) + mod(b)) % 1000000007;
}
ll prodmod(ll a, ll b)
{
    return (mod(a) * mod(b)) % 1000000007;
}
ll e(ll a, ll b)
{
    ll res = 1;
    ll temp = 1;
    if (b == 1)
        return a;
    if (b == 0)
        return 1;
    if (b % 2 == 0)
        res = e(a * a, b / 2);
    else
    {
        res *= a;
        temp = e(a, --b);
    }
    return res * temp;
}
ll emod(ll a, ll b)
{
    ll res = 1;
    ll temp = 1;
    if (b == 1)
        return a;
    if (b == 0)
        return 1;
    if (b % 2 == 0)
        res = emod(prodmod(a, a), b / 2);
    else
    {
        res = prodmod(a, res);
        temp = emod(a, --b);
    }
    return prodmod(res, temp);
}

template <class T>
void peakValley(vector<T> &arr)
{
    //int n;
    //cin>>n
    // vi arr(n+1);
    // fo(i,n){
    //     cin>>arr[i];
    // }
    // arr[n] = INT_MIN;
    // bool b = 1;
    // cout<<"local maxima :";
    // rep(i,0,n){
    //     if(arr[i]>arr[i+1]&&b){
    //         //local maxima
    //         b=0;
    //     }
    //     if(arr[i]<arr[i+1]&&!b){
    //         b=1;
    //         cout<<arr[i]<<" ";
    //         // local minim
    //     }
    // }
    int n = sz(arr);
    arr.resize(n + 2);
    rotate(arr.begin(), arr.begin() + n + 1, arr.end());
    arr[0] = arr[1];

    bool sign = 1;
    ll sum = 0;
    T lastValley = -1;
    T lastPeak = -1;
    T firstValley = -1;
    T firstPeak = -1;
    bool fp = 1;
    bool fv = 1;
    cout << "all valleys ";
    for (int i = 1; i < n + 2 - 1; i++)
    {
        //first peak;
        if (arr[i - 1] <= arr[i] and arr[i] >= arr[i + 1])
        {
            //i is the first peak
            if (fp)
            {
                firstPeak = arr[i];
                fp = 0;
            }
            sign = 0;
            sum += arr[i];
            lastPeak = arr[i];
        }
        if (arr[i - 1] >= arr[i] and arr[i] <= arr[i + 1])
        {
            //valley
            if (fv)
            {
                firstValley = arr[i];
                fv = 0;
            }
            cout << arr[i] << " ";
            sign = 1;
            sum -= arr[i];
            lastValley = arr[i];
        }
    }
    //if(sign) ->what is last visited sign = 0 lastVisited is a peak
    //if(firstPeak||firstValley||lastPeak||lastValley != -1) ->:
    //gives the first and last visited peaks and valleys
}
template <class T>
void findLocalMaximaMinima(vector<T> arr)
{
    int n = arr.size();
    vector<int> mx, mn;

    if (arr[0] > arr[1])
        mx.push_back(0);

    else if (arr[0] < arr[1])
        mn.push_back(0);

    for (int i = 1; i < n - 1; i++)
    {

        if ((arr[i - 1] > arr[i]) and
            (arr[i] < arr[i + 1]))
            mn.push_back(i);

        else if ((arr[i - 1] < arr[i]) and
                 (arr[i] > arr[i + 1]))
            mx.push_back(i);
    }
    if (arr[n - 1] > arr[n - 2])
        mx.push_back(n - 1);

    else if (arr[n - 1] < arr[n - 2])
        mn.push_back(n - 1);
}
template <class T>
bool ispeak(int i, vector<T> a)
{
    int n = a.size();
    if (i < 0 || i >= n)
        return 0;
    return ((i == 0 || a[i - 1] < a[i]) && ((i == n - 1) || a[i] > a[i + 1]));
}
template <class T>
bool islow(int i, vector<T> a)
{
    int n = a.size();
    if (i < 0 || i >= n)
        return 0;
    return ((i > 0 && i < n - 1 && a[i - 1] > a[i] && a[i + 1] > a[i]));
}
template <class T>
int digits(T a)
{
    vector<long long> dig = {
        0,
        9,
        99,
        999,
        9999,
        99999,
        999999,
        9999999,
        99999999,
        999999999,
        9999999999,
        99999999999,
        999999999999,
        9999999999999,
        99999999999999,
        999999999999999,
        9999999999999999,
        99999999999999999,
        999999999999999999,
        INT64_MAX};
    auto it = lower_bound(dig.begin(), dig.end(), a);
    int idx = (it - dig.begin());
    return idx;
}
template <class T>
int sumint(T n)
{
    int a = 1;
    int su = 0;
    while (n)
    {
        su += n % 10;
        n /= 10;
    }
    return su;
}
template <class T>
void split(char c, T s)
{
    int n = s.size();
    int i = 0;
    while ((cout << s[i], i += 1) && (i < n) && (cout << c << ' '))
        ;
}
template <class T>
ll fib(T a)
{
    return ((a * (a + 1)) / 2);
}

template <class T>
ll nC2(T a)
{
    return ((a * (a - 1)) / 2);
}

template <class T>
vector<T> &divisors(T n)
{
    vector<T> *res = new vector<T>;
    vector<T> &ret = *res;
    int R = sqrt(n);
    ret.resize(R);
    int start = 0;
    int end = R - 1;
    for (int i = 2; i <= R; i++)
    {
        if (n % i == 0)
        {
            ret[start] = i;
            start++;
            T etc = n / i;
            if (etc != i)
            {
                ret[end] = etc;
                end--;
            }
        }
    }
    int s = INT_MAX;
    int en = -1;
    for (int i = 0; i < R; i++)
    {
        if (!ret[i])
        {
            s = min(s, i);
            en = i;
        }
    }
    if (en - s >= 0)
        ret.erase(ret.begin() + s, ret.begin() + en + 1);
    return ret;
}
template <class T>
void pvec(vector<T> &vec)
{
    for (auto c : vec)
        cout << c << " ";
}
ll strsum(string s)
{
    ll ret = 0;
    rep(i, sz(s), 0)
    {
        ret += s[i] - '0';
    }
    return ret;
}
bool isPalindrome(string str)
{
    int l = 0;
    int h = sz(str) - 1;

    while (h > l)
    {
        if (str[l++] != str[h--])
        {

            return 0;
        }
    }
    return 1;
}
string addstr(string a, string b)
{
    ll m = sz(a);
    ll n = sz(b);
    ll z = max(n, m);
    string c = string(z, '0');
    bool carry = 0;
    while (n > 0 || m > 0)
    {
        int t = ((m - 1 >= 0) ? (a[m - 1] - '0') : 0) + ((n - 1 >= 0) ? (b[n - 1] - '0') : 0) + carry;
        carry = (t > 9) ? 1 : 0;
        z--;
        c[z] = '0' + (t % 10);
        n--;
        m--;
    }
    if (carry)
        c = "1" + c;
    return c;
}
template <class T>
bool ith(T a, int b)
{
    return ((a & ((ll)1 << (b - 1))));
}
//fxns : div2, mod, addmod, prodmod, bin_expmod, bin_exp as e()
// digits, sumint, split, fib, nC2, divisors,
// pvec, isPalindrome, strsum, addstr,  ith

vi presum(vi a)
{
    vi ret(sz(a));
    ll sum = 0;
    fo(i, sz(a))
    {
        sum += a[i];
        ret[i] = sum;
    }
    return ret;
}

void solve(int &test)
{
    int n;
    cin >> n;
    vi arr(n);
    fo(i, n) cin >> arr[i];
    sort(all(arr));
    vi arrr = presum(arr);
    // pvec(arrr);
    int m;
    cin >> m;
    int idx = 0;
    int maxn = 0;
    for(int i = 0 ;i <  m && idx<n; i++)
    {
        int a;
        cin >> a;
        int b;
        cin >> b;
        if (b >= arrr[idx] && a <= arrr[idx])
        {
            maxn = arrr[idx];
            idx++;
        }
    }
    if (idx == n)
    {
        cout << maxn;
    }
    else
    {
        cout << -1;
    }
    return;
}
int main()
{
    quickie;
    //txt;
    //freopen("ip.txt", "r", stdin);
    int test;
    test = 0;
    if (test)
        cin >> test;
    else
        test = 1;
    while (test--)
    {
        solve(test);
        newl;
    }
    //cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s."<<endl;
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef vector<int> vi;
typedef pair<int, int> pi;
typedef vector<ll> vl;
typedef long double ld;
#define txt                        \
    freopen("ip.txt", "r", stdin); \
    freopen("op.txt", "w", stdout)
#define a3 INT_MAX
#define i3 INT_MIN
#define a6 INT64_MAX
#define i6 INT64_MIN
#define F first
#define S second
#define pb push_back
#define sz(v) (int)v.size()
#define fo(i, n) for (int i = 0; i < (int)n; i++)
#define rep(i, k, n) for (int i = k < (int)n ? k : k - 1; k < (int)n ? i < (int)n : i >= (int)n; k < (int)n ? i += 1 : i -= 1)
#define quickie                       \
    ios_base::sync_with_stdio(false); \
    cin.tie(NULL);
#define deb(x) cout << (#x) << " " << (x) << "\n";
#define newl cout << "\n"
#define all(s) s.begin(), s.end()
#define pi 3.14159265358979323846
#define fp(x) fixed << setprecision(x) <<
#define ub(a, v) upper_bound(a.begin(), arr.end(), v)
#define lb(a, v) lower_bound(a.begin(), arr.end(), v)
#define w(t) while (t--)
#define ee in()
#define er ien()
int in()
{
    int res;
    cin >> res;
    return res;
}
ll ien()
{
    ll res;
    cin >> res;
    return res;
}
template <class T>
bool div2(T a)
{
    return !(a & 1);
}
ll mod(ll a)
{
    if (a < 0)
    {
        a += 1000000007;
    }
    return a % 1000000007;
}
ll addmod(ll a, ll b)
{
    return (mod(a) + mod(b)) % 1000000007;
}
ll prodmod(ll a, ll b)
{
    return (mod(a) * mod(b)) % 1000000007;
}
ll e(ll a, ll b)
{
    ll res = 1;
    ll temp = 1;
    if (b == 1)
        return a;
    if (b == 0)
        return 1;
    if (b % 2 == 0)
        res = e(a * a, b / 2);
    else
    {
        res *= a;
        temp = e(a, --b);
    }
    return res * temp;
}
ll emod(ll a, ll b)
{
    ll res = 1;
    ll temp = 1;
    if (b == 1)
        return a;
    if (b == 0)
        return 1;
    if (b % 2 == 0)
        res = emod(prodmod(a, a), b / 2);
    else
    {
        res = prodmod(a, res);
        temp = emod(a, --b);
    }
    return prodmod(res, temp);
}

template <class T>
void peakValley(vector<T> &arr)
{
    //int n;
    //cin>>n
    // vi arr(n+1);
    // fo(i,n){
    //     cin>>arr[i];
    // }
    // arr[n] = INT_MIN;
    // bool b = 1;
    // cout<<"local maxima :";
    // rep(i,0,n){
    //     if(arr[i]>arr[i+1]&&b){
    //         //local maxima
    //         b=0;
    //     }
    //     if(arr[i]<arr[i+1]&&!b){
    //         b=1;
    //         cout<<arr[i]<<" ";
    //         // local minim
    //     }
    // }
    int n = sz(arr);
    arr.resize(n + 2);
    rotate(arr.begin(), arr.begin() + n + 1, arr.end());
    arr[0] = arr[1];

    bool sign = 1;
    ll sum = 0;
    T lastValley = -1;
    T lastPeak = -1;
    T firstValley = -1;
    T firstPeak = -1;
    bool fp = 1;
    bool fv = 1;
    cout << "all valleys ";
    for (int i = 1; i < n + 2 - 1; i++)
    {
        //first peak;
        if (arr[i - 1] <= arr[i] and arr[i] >= arr[i + 1])
        {
            //i is the first peak
            if (fp)
            {
                firstPeak = arr[i];
                fp = 0;
            }
            sign = 0;
            sum += arr[i];
            lastPeak = arr[i];
        }
        if (arr[i - 1] >= arr[i] and arr[i] <= arr[i + 1])
        {
            //valley
            if (fv)
            {
                firstValley = arr[i];
                fv = 0;
            }
            cout << arr[i] << " ";
            sign = 1;
            sum -= arr[i];
            lastValley = arr[i];
        }
    }
    //if(sign) ->what is last visited sign = 0 lastVisited is a peak
    //if(firstPeak||firstValley||lastPeak||lastValley != -1) ->:
    //gives the first and last visited peaks and valleys
}
template <class T>
void findLocalMaximaMinima(vector<T> arr)
{
    int n = arr.size();
    vector<int> mx, mn;

    if (arr[0] > arr[1])
        mx.push_back(0);

    else if (arr[0] < arr[1])
        mn.push_back(0);

    for (int i = 1; i < n - 1; i++)
    {

        if ((arr[i - 1] > arr[i]) and
            (arr[i] < arr[i + 1]))
            mn.push_back(i);

        else if ((arr[i - 1] < arr[i]) and
                 (arr[i] > arr[i + 1]))
            mx.push_back(i);
    }
    if (arr[n - 1] > arr[n - 2])
        mx.push_back(n - 1);

    else if (arr[n - 1] < arr[n - 2])
        mn.push_back(n - 1);
}
template <class T>
bool ispeak(int i, vector<T> a)
{
    int n = a.size();
    if (i < 0 || i >= n)
        return 0;
    return ((i == 0 || a[i - 1] < a[i]) && ((i == n - 1) || a[i] > a[i + 1]));
}
template <class T>
bool islow(int i, vector<T> a)
{
    int n = a.size();
    if (i < 0 || i >= n)
        return 0;
    return ((i > 0 && i < n - 1 && a[i - 1] > a[i] && a[i + 1] > a[i]));
}
template <class T>
int digits(T a)
{
    vector<long long> dig = {
        0,
        9,
        99,
        999,
        9999,
        99999,
        999999,
        9999999,
        99999999,
        999999999,
        9999999999,
        99999999999,
        999999999999,
        9999999999999,
        99999999999999,
        999999999999999,
        9999999999999999,
        99999999999999999,
        999999999999999999,
        INT64_MAX};
    auto it = lower_bound(dig.begin(), dig.end(), a);
    int idx = (it - dig.begin());
    return idx;
}
template <class T>
int sumint(T n)
{
    int a = 1;
    int su = 0;
    while (n)
    {
        su += n % 10;
        n /= 10;
    }
    return su;
}
template <class T>
void split(char c, T s)
{
    int n = s.size();
    int i = 0;
    while ((cout << s[i], i += 1) && (i < n) && (cout << c << ' '))
        ;
}
template <class T>
ll fib(T a)
{
    return ((a * (a + 1)) / 2);
}

template <class T>
ll nC2(T a)
{
    return ((a * (a - 1)) / 2);
}

template <class T>
vector<T> &divisors(T n)
{
    vector<T> *res = new vector<T>;
    vector<T> &ret = *res;
    int R = sqrt(n);
    ret.resize(R);
    int start = 0;
    int end = R - 1;
    for (int i = 2; i <= R; i++)
    {
        if (n % i == 0)
        {
            ret[start] = i;
            start++;
            T etc = n / i;
            if (etc != i)
            {
                ret[end] = etc;
                end--;
            }
        }
    }
    int s = INT_MAX;
    int en = -1;
    for (int i = 0; i < R; i++)
    {
        if (!ret[i])
        {
            s = min(s, i);
            en = i;
        }
    }
    if (en - s >= 0)
        ret.erase(ret.begin() + s, ret.begin() + en + 1);
    return ret;
}
template <class T>
void pvec(vector<T> &vec)
{
    for (auto c : vec)
        cout << c << " ";
}
ll strsum(string s)
{
    ll ret = 0;
    rep(i, sz(s), 0)
    {
        ret += s[i] - '0';
    }
    return ret;
}
bool isPalindrome(string str)
{
    int l = 0;
    int h = sz(str) - 1;

    while (h > l)
    {
        if (str[l++] != str[h--])
        {

            return 0;
        }
    }
    return 1;
}
string addstr(string a, string b)
{
    ll m = sz(a);
    ll n = sz(b);
    ll z = max(n, m);
    string c = string(z, '0');
    bool carry = 0;
    while (n > 0 || m > 0)
    {
        int t = ((m - 1 >= 0) ? (a[m - 1] - '0') : 0) + ((n - 1 >= 0) ? (b[n - 1] - '0') : 0) + carry;
        carry = (t > 9) ? 1 : 0;
        z--;
        c[z] = '0' + (t % 10);
        n--;
        m--;
    }
    if (carry)
        c = "1" + c;
    return c;
}
template <class T>
bool ith(T a, int b)
{
    return ((a & ((ll)1 << (b - 1))));
}
//fxns : div2, mod, addmod, prodmod, bin_expmod, bin_exp as e()
// digits, sumint, split, fib, nC2, divisors,
// pvec, isPalindrome, strsum, addstr,  ith

vi presum(vi a)
{
    vi ret(sz(a));
    ll sum = 0;
    fo(i, sz(a))
    {
        sum += a[i];
        ret[i] = sum;
    }
    return ret;
}

void solve(int &test)
{
    int n;
    cin >> n;
    vi arr(n);
    ll sum = 0;
    fo(i, n) cin >> arr[i],sum+=arr[i];
    sort(all(arr));
    vi arrr = presum(arr);
    // pvec(arrr);
    int m;
    cin >> m;
    int idx = 0;
    int maxn = a3;
    for(int i = 0 ;i <  m && idx<n; i++)
    {
        int a;
        cin >> a;
        int b;
        cin >> b;
        if (b >= sum)
        {
            maxn = max(sum,(ll)a);
        }
    }
    if(maxn == a3){
        cout<<-1;

    }else
    {
        cout<<maxn;
    }

}
int main()
{
    quickie;
    //txt;
    //freopen("ip.txt", "r", stdin);
    int test;
    test = 0;
    if (test)
        cin >> test;
    else
        test = 1;
    while (test--)
    {
        solve(test);
        newl;
    }
    //cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s."<<endl;
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef vector<int> vi;
typedef pair<int, int> pi;
typedef vector<ll> vl;
typedef long double ld;
#define txt                        \
    freopen("ip.txt", "r", stdin); \
    freopen("op.txt", "w", stdout)
#define a3 INT_MAX
#define i3 INT_MIN
#define a6 INT64_MAX
#define i6 INT64_MIN
#define F first
#define S second
#define pb push_back
#define sz(v) (int)v.size()
#define fo(i, n) for (int i = 0; i < (int)n; i++)
#define rep(i, k, n) for (int i = k < (int)n ? k : k - 1; k < (int)n ? i < (int)n : i >= (int)n; k < (int)n ? i += 1 : i -= 1)
#define quickie                       \
    ios_base::sync_with_stdio(false); \
    cin.tie(NULL);
#define deb(x) cout << (#x) << " " << (x) << "\n";
#define newl cout << "\n"
#define all(s) s.begin(), s.end()
#define pi 3.14159265358979323846
#define fp(x) fixed << setprecision(x) <<
#define ub(a, v) upper_bound(a.begin(), arr.end(), v)
#define lb(a, v) lower_bound(a.begin(), arr.end(), v)
#define w(t) while (t--)
#define ee in()
#define er ien()
int in()
{
    int res;
    cin >> res;
    return res;
}
ll ien()
{
    ll res;
    cin >> res;
    return res;
}
template <class T>
bool div2(T a)
{
    return !(a & 1);
}
ll mod(ll a)
{
    if (a < 0)
    {
        a += 1000000007;
    }
    return a % 1000000007;
}
ll addmod(ll a, ll b)
{
    return (mod(a) + mod(b)) % 1000000007;
}
ll prodmod(ll a, ll b)
{
    return (mod(a) * mod(b)) % 1000000007;
}
ll e(ll a, ll b)
{
    ll res = 1;
    ll temp = 1;
    if (b == 1)
        return a;
    if (b == 0)
        return 1;
    if (b % 2 == 0)
        res = e(a * a, b / 2);
    else
    {
        res *= a;
        temp = e(a, --b);
    }
    return res * temp;
}
ll emod(ll a, ll b)
{
    ll res = 1;
    ll temp = 1;
    if (b == 1)
        return a;
    if (b == 0)
        return 1;
    if (b % 2 == 0)
        res = emod(prodmod(a, a), b / 2);
    else
    {
        res = prodmod(a, res);
        temp = emod(a, --b);
    }
    return prodmod(res, temp);
}

template <class T>
void peakValley(vector<T> &arr)
{
    //int n;
    //cin>>n
    // vi arr(n+1);
    // fo(i,n){
    //     cin>>arr[i];
    // }
    // arr[n] = INT_MIN;
    // bool b = 1;
    // cout<<"local maxima :";
    // rep(i,0,n){
    //     if(arr[i]>arr[i+1]&&b){
    //         //local maxima
    //         b=0;
    //     }
    //     if(arr[i]<arr[i+1]&&!b){
    //         b=1;
    //         cout<<arr[i]<<" ";
    //         // local minim
    //     }
    // }
    int n = sz(arr);
    arr.resize(n + 2);
    rotate(arr.begin(), arr.begin() + n + 1, arr.end());
    arr[0] = arr[1];

    bool sign = 1;
    ll sum = 0;
    T lastValley = -1;
    T lastPeak = -1;
    T firstValley = -1;
    T firstPeak = -1;
    bool fp = 1;
    bool fv = 1;
    cout << "all valleys ";
    for (int i = 1; i < n + 2 - 1; i++)
    {
        //first peak;
        if (arr[i - 1] <= arr[i] and arr[i] >= arr[i + 1])
        {
            //i is the first peak
            if (fp)
            {
                firstPeak = arr[i];
                fp = 0;
            }
            sign = 0;
            sum += arr[i];
            lastPeak = arr[i];
        }
        if (arr[i - 1] >= arr[i] and arr[i] <= arr[i + 1])
        {
            //valley
            if (fv)
            {
                firstValley = arr[i];
                fv = 0;
            }
            cout << arr[i] << " ";
            sign = 1;
            sum -= arr[i];
            lastValley = arr[i];
        }
    }
    //if(sign) ->what is last visited sign = 0 lastVisited is a peak
    //if(firstPeak||firstValley||lastPeak||lastValley != -1) ->:
    //gives the first and last visited peaks and valleys
}
template <class T>
void findLocalMaximaMinima(vector<T> arr)
{
    int n = arr.size();
    vector<int> mx, mn;

    if (arr[0] > arr[1])
        mx.push_back(0);

    else if (arr[0] < arr[1])
        mn.push_back(0);

    for (int i = 1; i < n - 1; i++)
    {

        if ((arr[i - 1] > arr[i]) and
            (arr[i] < arr[i + 1]))
            mn.push_back(i);

        else if ((arr[i - 1] < arr[i]) and
                 (arr[i] > arr[i + 1]))
            mx.push_back(i);
    }
    if (arr[n - 1] > arr[n - 2])
        mx.push_back(n - 1);

    else if (arr[n - 1] < arr[n - 2])
        mn.push_back(n - 1);
}
template <class T>
bool ispeak(int i, vector<T> a)
{
    int n = a.size();
    if (i < 0 || i >= n)
        return 0;
    return ((i == 0 || a[i - 1] < a[i]) && ((i == n - 1) || a[i] > a[i + 1]));
}
template <class T>
bool islow(int i, vector<T> a)
{
    int n = a.size();
    if (i < 0 || i >= n)
        return 0;
    return ((i > 0 && i < n - 1 && a[i - 1] > a[i] && a[i + 1] > a[i]));
}
template <class T>
int digits(T a)
{
    vector<long long> dig = {
        0,
        9,
        99,
        999,
        9999,
        99999,
        999999,
        9999999,
        99999999,
        999999999,
        9999999999,
        99999999999,
        999999999999,
        9999999999999,
        99999999999999,
        999999999999999,
        9999999999999999,
        99999999999999999,
        999999999999999999,
        INT64_MAX};
    auto it = lower_bound(dig.begin(), dig.end(), a);
    int idx = (it - dig.begin());
    return idx;
}
template <class T>
int sumint(T n)
{
    int a = 1;
    int su = 0;
    while (n)
    {
        su += n % 10;
        n /= 10;
    }
    return su;
}
template <class T>
void split(char c, T s)
{
    int n = s.size();
    int i = 0;
    while ((cout << s[i], i += 1) && (i < n) && (cout << c << ' '))
        ;
}
template <class T>
ll fib(T a)
{
    return ((a * (a + 1)) / 2);
}

template <class T>
ll nC2(T a)
{
    return ((a * (a - 1)) / 2);
}

template <class T>
vector<T> &divisors(T n)
{
    vector<T> *res = new vector<T>;
    vector<T> &ret = *res;
    int R = sqrt(n);
    ret.resize(R);
    int start = 0;
    int end = R - 1;
    for (int i = 2; i <= R; i++)
    {
        if (n % i == 0)
        {
            ret[start] = i;
            start++;
            T etc = n / i;
            if (etc != i)
            {
                ret[end] = etc;
                end--;
            }
        }
    }
    int s = INT_MAX;
    int en = -1;
    for (int i = 0; i < R; i++)
    {
        if (!ret[i])
        {
            s = min(s, i);
            en = i;
        }
    }
    if (en - s >= 0)
        ret.erase(ret.begin() + s, ret.begin() + en + 1);
    return ret;
}
template <class T>
void pvec(vector<T> &vec)
{
    for (auto c : vec)
        cout << c << " ";
}
ll strsum(string s)
{
    ll ret = 0;
    rep(i, sz(s), 0)
    {
        ret += s[i] - '0';
    }
    return ret;
}
bool isPalindrome(string str)
{
    int l = 0;
    int h = sz(str) - 1;

    while (h > l)
    {
        if (str[l++] != str[h--])
        {

            return 0;
        }
    }
    return 1;
}
string addstr(string a, string b)
{
    ll m = sz(a);
    ll n = sz(b);
    ll z = max(n, m);
    string c = string(z, '0');
    bool carry = 0;
    while (n > 0 || m > 0)
    {
        int t = ((m - 1 >= 0) ? (a[m - 1] - '0') : 0) + ((n - 1 >= 0) ? (b[n - 1] - '0') : 0) + carry;
        carry = (t > 9) ? 1 : 0;
        z--;
        c[z] = '0' + (t % 10);
        n--;
        m--;
    }
    if (carry)
        c = "1" + c;
    return c;
}
template <class T>
bool ith(T a, int b)
{
    return ((a & ((ll)1 << (b - 1))));
}
//fxns : div2, mod, addmod, prodmod, bin_expmod, bin_exp as e()
// digits, sumint, split, fib, nC2, divisors,
// pvec, isPalindrome, strsum, addstr,  ith

vi presum(vi a)
{
    vi ret(sz(a));
    ll sum = 0;
    fo(i, sz(a))
    {
        sum += a[i];
        ret[i] = sum;
    }
    return ret;
}

void solve(int &test)
{
    int n;
    cin >> n;
    vi arr(n);
    fo(i, n) cin >> arr[i];
    // sort(all(arr));
    vi arrr = presum(arr);
    // pvec(arrr);
    int m;
    cin >> m;
    int idx = 0;
    int maxn = 0;
    for(int i = 0 ;i <  m && idx<n; i++)
    {
        int a;
        cin >> a;
        int b;
        cin >> b;
        if (b >= arrr[idx] && a <= arrr[idx])
        {
            maxn = arrr[idx];
            idx++;
        }
    }
    if (idx == n)
    {
        cout << maxn;
    }
    else
    {
        cout << -1;
    }
    return;
}
int main()
{
    quickie;
    //txt;
    //freopen("ip.txt", "r", stdin);
    int test;
    test = 0;
    if (test)
        cin >> test;
    else
        test = 1;
    while (test--)
    {
        solve(test);
        newl;
    }
    //cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s."<<endl;
    return 0;
}


#include <iostream>
#include <vector>
#include <set>
#include <map>
#include <string>
#include <algorithm>
#include <queue>
#include <iomanip>
#include <stack>

#define pb push_back
#define ins insert
#define pob pop_back
#define er erase
#define emp empty
#define f first
#define s second
#define ff f.f
#define fs f.s
#define sf s.f
#define ss s.s
#define var auto

using namespace std;
template<typename  T>
using s = set<T>;
template<typename T>
using  v = vector<T>;
template<typename T1, typename  T2>
using p = pair < T1, T2>;
typedef long long ll;
typedef p<int, int> pii;
typedef v<int> vi;
typedef v<pii> vpii;
typedef s<int> si;
typedef s<pii> spii;

const int Max = 1e5 + 100;
const int Mx = 2e1 + 10;
const int Len = 1e6;
const int MOD = 1e9 + 7;
const int INF = 1000000000;

int n, m, q;
vi st_edPoint;
int main()
{
	cin >> n;
	int sumT = 0;
	for (int i = 0; i < n; i++)
	{
		int a; cin >> a;
		sumT += a;
	}
	cin >> m;
	for (int i = 0; i < m; i++)
	{
		int a, b; cin >> a >> b;
		st_edPoint.pb(a);
		st_edPoint.pb(b);
	}

	for (int i = 0; i < 2 * m - 1; i += 2)
	{
		int strt = st_edPoint[i];
		int end = st_edPoint[i + 1];
		int nxtStrt = INF;
		if (i < 2 * m - 2)
			nxtStrt = st_edPoint[i + 2];

		if (sumT <= end)
		{
			if (sumT >= strt)
			{
				cout << sumT << '\n';
				break;
			}
		}
		else if (sumT <= nxtStrt)
		{
			cout << nxtStrt << '\n';
			break;
		}
	}
}


#include <iostream>
#include <vector>
#include <set>
#include <map>
#include <string>
#include <algorithm>
#include <queue>
#include <iomanip>
#include <stack>

#define pb push_back
#define ins insert
#define pob pop_back
#define er erase
#define emp empty
#define f first
#define s second
#define ff f.f
#define fs f.s
#define sf s.f
#define ss s.s
#define var auto

using namespace std;
template<typename  T>
using s = set<T>;
template<typename T>
using  v = vector<T>;
template<typename T1, typename  T2>
using p = pair < T1, T2>;
typedef long long ll;
typedef p<int, int> pii;
typedef v<int> vi;
typedef v<pii> vpii;
typedef s<int> si;
typedef s<pii> spii;

const int Max = 1e5 + 100;
const int Mx = 2e1 + 10;
const int Len = 1e6;
const int MOD = 1e9 + 7;
const int INF = 1000000000;

int n, m, q;
vi st_edPoint;
int main()
{
	cin >> n;
	int sumT = 0;
	for (int i = 0; i < n; i++)
	{
		int a; cin >> a;
		sumT += a;
	}
	cin >> m;
	for (int i = 0; i < m; i++)
	{
		int a, b; cin >> a >> b;
		st_edPoint.pb(a);
		st_edPoint.pb(b);
	}

	for (int i = 0; i < 2 * m - 1; i += 2)
	{
		int strt = st_edPoint[i];
		int end = st_edPoint[i + 1];
		int nxtStrt = INF;
		if (i < 2 * m - 2)
			nxtStrt = st_edPoint[i + 2];

		if (sumT <= end)
		{
			if (sumT >= strt)
			{
				cout << sumT << '\n';
				break;
			}
		}
		else if (sumT <= nxtStrt)
		{
			if (nxtStrt == INF)
				cout << -1 << '\n';
			else
				cout << nxtStrt << '\n';
			break;
		}
	}
}


#include <iostream>
#include <vector>
#include <set>
#include <map>
#include <string>
#include <algorithm>
#include <queue>
#include <iomanip>
#include <stack>

#define pb push_back
#define ins insert
#define pob pop_back
#define er erase
#define emp empty
#define f first
#define s second
#define ff f.f
#define fs f.s
#define sf s.f
#define ss s.s
#define var auto

using namespace std;
template<typename  T>
using s = set<T>;
template<typename T>
using  v = vector<T>;
template<typename T1, typename  T2>
using p = pair < T1, T2>;
typedef long long ll;
typedef p<int, int> pii;
typedef v<int> vi;
typedef v<pii> vpii;
typedef s<int> si;
typedef s<pii> spii;

const int Max = 1e5 + 100;
const int Mx = 2e1 + 10;
const int Len = 1e6;
const int MOD = 1e9 + 7;
const int INF = 1000000000;

int n, m, q;
int main()
{
	cin >> n;
	ll sumT = 0;
	for (int i = 0; i < n; i++)
	{
		int a; cin >> a;
		sumT += a;
	}
	cin >> m;
	bool foundAns = false;
	for (int i = 0; i < m; i++)
	{
		int a, b; cin >> a >> b;

		if (!foundAns)
		{
			if (a <= sumT && sumT <= b)
			{
				cout << sumT;
				foundAns = true;
			}
			else if (sumT < a)
			{
				cout << a;
				foundAns = true;
			}
		}
	}

	if (!foundAns)
		cout << -1;
}


#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
#include <cmath>
#define DEBUG(x) std::cout << '>' << #x << ':' << x << '\n';
#define PRINT(x) for (int i = 0; i < (int)x.size(); i++) std::cout << x[i] << ' '; std::cout << '\n';
#define io std::ios_base::sync_with_stdio(false);std::cin.tie(NULL);
const int INF = 1<<29;
typedef long long ll;

int solve() {
    int n,m,in,in2;
    std::cin >> n;
    ll sum = 0;
    for (int i = 0; i < n; i++) {
        std::cin >> in;
        sum += in;
    }
    std::cin >> m;
    for (int i = 0; i < m; i++) {
        std::cin >> in >> in2;
        if (in >= sum)
            return in;
        else if (in <= sum && in2 >= sum)
            return sum;
    }
    return -1;
}

int main()
{
	io;
    #ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    #endif

    std::cout << solve();

    #ifndef ONLINE_JUDGE
    std::cout << '\n' << "Time elapsed : " << 1.0 * clock() / CLOCKS_PER_SEC << " sec \n";
    #endif
    return 0;
}


#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
#include <cmath>
#define DEBUG(x) std::cout << '>' << #x << ':' << x << '\n';
#define PRINT(x) for (int i = 0; i < (int)x.size(); i++) std::cout << x[i] << ' '; std::cout << '\n';
#define io std::ios_base::sync_with_stdio(false);std::cin.tie(NULL);
const int INF = 1<<29;
typedef long long ll;

int solve(std::vector<int> v, std::vector<std::pair<int,int>> t, int n, int m) {
    if (m == 0) return -1;
    int tidx = 0;
    int sum = 0;
    for (int i = 0; i < n; i++) {
        sum += v[i];
        while (sum > t[tidx].second && tidx < m)
            tidx++;
        if (tidx == m)
            if (sum > t[tidx-1].second)
                return -1;
    }
    return t[tidx].first + sum - t[tidx].first;
}

int main()
{
	io;
    #ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    #endif

    int n;
    std::cin >> n;
    std::vector<int> v(n);
    for (int &it : v)
        std::cin >> it;
    int m;
    std::cin >> m;
    std::vector<std::pair<int, int>> t(m);
    for (int i = 0; i < m; i++)
        std::cin >> t[i].first >> t[i].second;
    std::cout << solve(v,t,n,m);

    #ifndef ONLINE_JUDGE
    std::cout << '\n' << "Time elapsed : " << 1.0 * clock() / CLOCKS_PER_SEC << " sec \n";
    #endif
    return 0;
}


#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
#include <cmath>
#define DEBUG(x) std::cout << '>' << #x << ':' << x << '\n';
#define PRINT(x) for (int i = 0; i < (int)x.size(); i++) std::cout << x[i] << ' '; std::cout << '\n';
#define io std::ios_base::sync_with_stdio(false);std::cin.tie(NULL);
const int INF = 1<<29;
typedef long long ll;

int solve(std::vector<int> v, std::vector<std::pair<int,int>> t, int n, int m) {
    int tidx = 0;
    int sum = 0;
    for (int i = 0; i < n; i++) {
        sum += v[i];
        while (sum > t[tidx].second && tidx < m)
            tidx++;
        if (tidx == m)
            if (sum > t[tidx-1].second)
                return -1;
    }
    return t[tidx].first + sum - t[tidx].first;
}

int main()
{
	io;
    #ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    #endif

    int n;
    std::cin >> n;
    std::vector<int> v(n);
    for (int &it : v)
        std::cin >> it;
    int m;
    std::cin >> m;
    std::vector<std::pair<int, int>> t(m);
    for (int i = 0; i < m; i++)
        std::cin >> t[i].first >> t[i].second;
    std::cout << solve(v,t,n,m);

    #ifndef ONLINE_JUDGE
    std::cout << '\n' << "Time elapsed : " << 1.0 * clock() / CLOCKS_PER_SEC << " sec \n";
    #endif
    return 0;
}


#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
#include <cmath>
#define DEBUG(x) std::cout << '>' << #x << ':' << x << '\n';
#define PRINT(x) for (int i = 0; i < (int)x.size(); i++) std::cout << x[i] << ' '; std::cout << '\n';
#define io std::ios_base::sync_with_stdio(false);std::cin.tie(NULL);
const int INF = 1<<29;
typedef long long ll;

int solve() {
    int n,m,in,in2;
    std::cin >> n;
    ll sum = 0;
    for (int i = 0; i < n; i++) {
        std::cin >> in;
        sum += in;
    }
    for (int i = 0; i < m; i++) {
        std::cin >> in >> in2;
        if (in >= sum)
            return in;
        else if (in <= sum && in2 >= sum)
            return sum;
    }
    return -1;
}

int main()
{
	io;
    #ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    #endif

    std::cout << solve();

    #ifndef ONLINE_JUDGE
    std::cout << '\n' << "Time elapsed : " << 1.0 * clock() / CLOCKS_PER_SEC << " sec \n";
    #endif
    return 0;
}


#include <bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define fr first
#define sc second
#define MOD 1000000007
#define len(x) x.size()
#define min3(a, b, c) min(a, min(b, c))
#define max3(a, b, c) max(a, max(b, c))
#define all(v) v.begin(), v.end()
#define alla(a,n) a, a + n
#define f(i, n) for (int i = 0; i < int(n); i++)
#define g(i, n) for (int i = 0; i < int(n-1); i++)
#define fj(j, n) for (int j = 0; j < int(n); j++)
#define fio ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL)

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> pll;
typedef vector<ll> vll;
typedef vector<pll> vpll;
typedef vector<vll> vvll;
typedef vector<string> vs;

// Driver function to sort the vector elements by
// second element of pair in descending order
bool sortbysecdesc(const pair<int,int> &a,
                   const pair<int,int> &b)
{
       return a.second>b.second;
}

// Driver function to sort the vector elements by
// first element of pair in descending order
bool sortinrev(const pair<int,int> &a,
               const pair<int,int> &b)
{
       return (a.first > b.first);
}

// Function to return gcd of a and b
ll gcd(ll a, ll b)
{
    if (a == 0)
        return b;
    return gcd(b % a, a);
}

//Function to return lcm of a and b
ll lcm(ll a,ll b)
{
    return (a*b)/gcd(a,b);
}

// Function to find gcd of array of
// numbers
int findGCD(ll a[], ll n)
{
    int result = a[0];
    for (int i = 1; i < n; i++)
    {
        result = gcd(a[i], result);

        if(result == 1)
        {
           return 1;
        }
    }
    return result;
}

int32_t main() {
    fio;

   ll n,sum=0,p,q;
   cin>>n;
   ll a[n];
   f(i,n)
   {
   cin>>a[i];
   sum+=a[i];
   }
   sort(a,a+n);
   ll m;
   cin>>m;
   ll l[m],r[m];
   f(i,m)
   {
       cin>>p>>q;
       if(sum<=q)
       {
       cout<<max(p,sum)<<endl;
       return 0;
       }

   }
// //    if(m==0)
// //    cout<<sum<<endl;
//    if(a[n-1]>r[n-1] || m==0)
//    cout<<-1<<endl;
//    else
//    cout<<sum<<endl;

cout<<-1<<endl;

    return 0 ;

}


#include <bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define fr first
#define sc second
#define MOD 1000000007
#define len(x) x.size()
#define min3(a, b, c) min(a, min(b, c))
#define max3(a, b, c) max(a, max(b, c))
#define all(v) v.begin(), v.end()
#define alla(a,n) a, a + n
#define f(i, n) for (int i = 0; i < int(n); i++)
#define g(i, n) for (int i = 0; i < int(n-1); i++)
#define fj(j, n) for (int j = 0; j < int(n); j++)
#define fio ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL)

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> pll;
typedef vector<ll> vll;
typedef vector<pll> vpll;
typedef vector<vll> vvll;
typedef vector<string> vs;

// Driver function to sort the vector elements by
// second element of pair in descending order
bool sortbysecdesc(const pair<int,int> &a,
                   const pair<int,int> &b)
{
       return a.second>b.second;
}

// Driver function to sort the vector elements by
// first element of pair in descending order
bool sortinrev(const pair<int,int> &a,
               const pair<int,int> &b)
{
       return (a.first > b.first);
}

// Function to return gcd of a and b
ll gcd(ll a, ll b)
{
    if (a == 0)
        return b;
    return gcd(b % a, a);
}

//Function to return lcm of a and b
ll lcm(ll a,ll b)
{
    return (a*b)/gcd(a,b);
}

// Function to find gcd of array of
// numbers
int findGCD(ll a[], ll n)
{
    int result = a[0];
    for (int i = 1; i < n; i++)
    {
        result = gcd(a[i], result);

        if(result == 1)
        {
           return 1;
        }
    }
    return result;
}

int32_t main() {
    fio;

   ll n,sum=0;
   cin>>n;
   ll a[n];
   f(i,n)
   {
   cin>>a[i];
   sum+=a[i];
   }
   sort(a,a+n);
   ll m;
   cin>>m;
   ll l[m],r[m];
   f(i,m)
   {
       cin>>l[i]>>r[i];
   }
   if(m==0)
   cout<<sum<<endl;
   else if(a[n-1]>r[n-1])
   cout<<-1<<endl;
   else
   cout<<sum<<endl;

    return 0 ;

}


#include <bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define fr first
#define sc second
#define MOD 1000000007
#define len(x) x.size()
#define min3(a, b, c) min(a, min(b, c))
#define max3(a, b, c) max(a, max(b, c))
#define all(v) v.begin(), v.end()
#define alla(a,n) a, a + n
#define f(i, n) for (int i = 0; i < int(n); i++)
#define g(i, n) for (int i = 0; i < int(n-1); i++)
#define fj(j, n) for (int j = 0; j < int(n); j++)
#define fio ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL)

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> pll;
typedef vector<ll> vll;
typedef vector<pll> vpll;
typedef vector<vll> vvll;
typedef vector<string> vs;

// Driver function to sort the vector elements by
// second element of pair in descending order
bool sortbysecdesc(const pair<int,int> &a,
                   const pair<int,int> &b)
{
       return a.second>b.second;
}

// Driver function to sort the vector elements by
// first element of pair in descending order
bool sortinrev(const pair<int,int> &a,
               const pair<int,int> &b)
{
       return (a.first > b.first);
}

// Function to return gcd of a and b
ll gcd(ll a, ll b)
{
    if (a == 0)
        return b;
    return gcd(b % a, a);
}

//Function to return lcm of a and b
ll lcm(ll a,ll b)
{
    return (a*b)/gcd(a,b);
}

// Function to find gcd of array of
// numbers
int findGCD(ll a[], ll n)
{
    int result = a[0];
    for (int i = 1; i < n; i++)
    {
        result = gcd(a[i], result);

        if(result == 1)
        {
           return 1;
        }
    }
    return result;
}

int32_t main() {
    fio;

   ll n,sum=0;
   cin>>n;
   ll a[n];
   f(i,n)
   {
   cin>>a[i];
   sum+=a[i];
   }
   sort(a,a+n);
   ll m;
   cin>>m;
   ll l[m],r[m];
   f(i,m)
   {
       cin>>l[i]>>r[i];
   }
   if(a[n-1]>r[n-1])
   cout<<-1<<endl;
   else
   cout<<sum<<endl;

    return 0 ;

}


#include <bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define fr first
#define sc second
#define MOD 1000000007
#define len(x) x.size()
#define min3(a, b, c) min(a, min(b, c))
#define max3(a, b, c) max(a, max(b, c))
#define all(v) v.begin(), v.end()
#define alla(a,n) a, a + n
#define f(i, n) for (int i = 0; i < int(n); i++)
#define g(i, n) for (int i = 0; i < int(n-1); i++)
#define fj(j, n) for (int j = 0; j < int(n); j++)
#define fio ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL)

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> pll;
typedef vector<ll> vll;
typedef vector<pll> vpll;
typedef vector<vll> vvll;
typedef vector<string> vs;

// Driver function to sort the vector elements by
// second element of pair in descending order
bool sortbysecdesc(const pair<int,int> &a,
                   const pair<int,int> &b)
{
       return a.second>b.second;
}

// Driver function to sort the vector elements by
// first element of pair in descending order
bool sortinrev(const pair<int,int> &a,
               const pair<int,int> &b)
{
       return (a.first > b.first);
}

// Function to return gcd of a and b
ll gcd(ll a, ll b)
{
    if (a == 0)
        return b;
    return gcd(b % a, a);
}

//Function to return lcm of a and b
ll lcm(ll a,ll b)
{
    return (a*b)/gcd(a,b);
}

// Function to find gcd of array of
// numbers
int findGCD(ll a[], ll n)
{
    int result = a[0];
    for (int i = 1; i < n; i++)
    {
        result = gcd(a[i], result);

        if(result == 1)
        {
           return 1;
        }
    }
    return result;
}

int32_t main() {
    fio;

   ll n,sum=0;
   cin>>n;
   ll a[n];
   f(i,n)
   {
   cin>>a[i];
   sum+=a[i];
   }
   sort(a,a+n);
   ll m;
   cin>>m;
   ll l[m],r[m];
   f(i,m)
   {
       cin>>l[i]>>r[i];
   }
//    if(m==0)
//    cout<<sum<<endl;
   if(a[n-1]>r[n-1] || m==0)
   cout<<-1<<endl;
   else
   cout<<sum<<endl;

    return 0 ;

}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define sidha(i,a,b) for(int i=a;i<b;i++)
#define vi vector<int>
#define pb push_back
#define st string
#define pairi pair<int,int>

int solve(int sum,int m){
	if(m!=0){
		for(int i=0;i<m;i++){
			int a,b;cin>>a>>b;
			if(sum>=a && sum<=b){
				return sum;
			}
			else if(sum<a){
				return a;
			}
		}
	}
	return -1;
}

int main(){
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
	int n;cin>>n;
	int arr[n];
	ll sum=0;
	sidha(i,0,n){
		cin>>arr[i];
		sum+=arr[i];
	}
	int m;cin>>m;
	cout<<solve(sum,m)<<endl;
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define sidha(i,a,b) for(int i=a;i<b;i++)
#define vi vector<int>
#define pb push_back
#define st string
#define pairi pair<int,int>

int main(){
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
	int n;cin>>n;
	int arr[n];
	ll sum=0;
	sidha(i,0,n){
		cin>>arr[i];
		sum+=arr[i];
	}
	int m;cin>>m;
	int brr[2*m];
	sidha(i,0,2*m){
		cin>>brr[i];
	}
	for(int i=0;i<2*m;i+=2){
		if(sum>=brr[i] && sum<=brr[i+1]){
			cout<<sum<<endl;
			break;
		}
		else if(sum>brr[i-1] && sum<brr[i]){
			cout<<brr[i]<<endl;
			break;
		}
		else if(sum>brr[2*m-1]){
			cout<<"-1"<<endl;
		}
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define sidha(i,a,b) for(int i=a;i<b;i++)
#define vi vector<int>
#define pb push_back
#define st string
#define pairi pair<int,int>

int main(){
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
	int n;cin>>n;
	int arr[n];
	ll sum=0;
	sidha(i,0,n){
		cin>>arr[i];
		sum+=arr[i];
	}
	int m;cin>>m;
	if(m==0){
		cout<<"-1"<<endl;
	}
	else{
	int brr[2*m];
	sidha(i,0,2*m){
		cin>>brr[i];
	}
	for(int i=0;i<2*m;i+=2){
		if(sum>=brr[i] && sum<=brr[i+1]){
			cout<<sum<<endl;
			break;
		}
		else if(sum>brr[i-1] && sum<brr[i]){
			cout<<brr[i]<<endl;
			break;
		}
		else if(sum>brr[2*m-1] || m==0){
			cout<<"-1"<<endl;
		}
	}
	}
	return 0;
}


#include<bits/stdc++.h>
#define int long long
#define vctri vector<int>
#define vctrc vector<char>
#define bruh std::ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
#define pb push_back
#define forn(i,n) for(int i=0;i<n;i++)
#define loopi(i,x, n) for(int i=x ; i < n; i++)
#define loopd(i,x, n) for(int i=x ; i >= n; i--)
#define forv(itr,vc) for (vector<int>::iterator itr=vc.begin();itr!=vc.end();itr++)
using namespace std;
bool testcase=0;
void solve(){
    int n;
    cin>>n;
    int sum=0;
    int t;
    forn(i,n){
        cin>>t;
        sum+=t;
    }
    int k;
    cin>>k;
    int r,l;
    while(k--) {
        cin>>l>>r;
        if(r>=sum) {
            int y=l>sum?l:sum;
            cout<<y;
        }
    }
}
int32_t main()
{
    #ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("outpu.txt", "w", stdout);
    #endif
    bruh
    int k;
    if (testcase){
    cin>>k;
    }
    else k=1;
    while(k--){
        solve();
        cout<<endl;
        }
        return 0;
    }


#include<bits/stdc++.h>
#define int long long
#define vctri vector<int>
#define vctrc vector<char>
#define bruh std::ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
#define pb push_back
#define forn(i,n) for(int i=0;i<n;i++)
#define loopi(i,x, n) for(int i=x ; i < n; i++)
#define loopd(i,x, n) for(int i=x ; i >= n; i--)
#define forv(itr,vc) for (vector<int>::iterator itr=vc.begin();itr!=vc.end();itr++)
using namespace std;
bool testcase=0;
void solve(){
    int n;
    cin>>n;
    int sum=0;
    int t;
    forn(i,n){
        cin>>t;
        sum+=t;
    }
    int k;
    cin>>k;
    int r,l;
    while(k--) {
        cin>>l>>r;
        if(r>=sum) {
            int y=l>sum?l:sum;
            cout<<y;
            return;
        }
    }
    cout<<-1;
}
int32_t main()
{
    #ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("outpu.txt", "w", stdout);
    #endif
    bruh
    int k;
    if (testcase){
    cin>>k;
    }
    else k=1;
    while(k--){
        solve();
        cout<<endl;
        }
        return 0;
    }


#include<bits/stdc++.h>
#define int long long
#define vctri vector<int>
#define vctrc vector<char>
#define bruh std::ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
#define pb push_back
#define forn(i,n) for(int i=0;i<n;i++)
#define loopi(i,x, n) for(int i=x ; i < n; i++)
#define loopd(i,x, n) for(int i=x ; i >= n; i--)
#define forv(itr,vc) for (vector<int>::iterator itr=vc.begin();itr!=vc.end();itr++)
using namespace std;
bool testcase=0;
void solve(){
    int n;
    cin>>n;
    int sum=0;
    int t;
    forn(i,n){
        cin>>t;
        sum+=t;
    }
    int k;
    cin>>k;
    int flag=0;
    int al,ar,pl,pr;
    forn(i,n){
        int l,r;
        cin>>l>>r;
        if(sum>=l and sum<=r){
            pl=l;
            pr=r;
            flag=1;
        }
        if(flag){
            al=l;ar=r;
            flag=0;
        }

    }
    if(sum>=pl and sum<=pr)
        cout<<sum;
    else if (sum>=pl)
        cout<<al;
    else
        cout<<-1;
}
int32_t main()
{
    #ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("outpu.txt", "w", stdout);
    #endif
    bruh
    int k;
    if (testcase){
    cin>>k;
    }
    else k=1;
    while(k--){
        solve();
        cout<<endl;
        }
        return 0;
    }


#include<bits/stdc++.h>
#define int long long
#define vctri vector<int>
#define vctrc vector<char>
#define bruh std::ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
#define pb push_back
#define forn(i,n) for(int i=0;i<n;i++)
#define loopi(i,x, n) for(int i=x ; i < n; i++)
#define loopd(i,x, n) for(int i=x ; i >= n; i--)
#define forv(itr,vc) for (vector<int>::iterator itr=vc.begin();itr!=vc.end();itr++)
using namespace std;
bool testcase=0;
void solve(){
    int n;
    cin>>n;
    int sum=0;
    int t;
    forn(i,n){
        cin>>t;
        sum+=t;
    }
    int k;
    cin>>k;
    int r,l;
    while(k--) {
        cin>>l>>r;
        if(r>=sum) {
            cout<<l>sum?l:sum;
        }
    }
}
int32_t main()
{
    #ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("outpu.txt", "w", stdout);
    #endif
    bruh
    int k;
    if (testcase){
    cin>>k;
    }
    else k=1;
    while(k--){
        solve();
        cout<<endl;
        }
        return 0;
    }


#include<bits/stdc++.h>
#define int long long
#define vctri vector<int>
#define vctrc vector<char>
#define bruh std::ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
#define pb push_back
#define forn(i,n) for(int i=0;i<n;i++)
#define loopi(i,x, n) for(int i=x ; i < n; i++)
#define loopd(i,x, n) for(int i=x ; i >= n; i--)
#define forv(itr,vc) for (vector<int>::iterator itr=vc.begin();itr!=vc.end();itr++)
using namespace std;
bool testcase=0;
void solve(){
    int n;
    cin>>n;
    int sum=0;
    forn(i,n){
        int t;
        cin>>t;
        sum+=t;
    }
    int k;
    cin>>k;
    int flag=0;
    forn(i,n){
        int l,r;
        cin>>l>>r;
        if(sum>=l and sum<=r)
            flag=1;
    }
    if(flag)
        cout<<sum;
    else
        cout<<-1;
}
int32_t main()
{
    #ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("outpu.txt", "w", stdout);
    #endif
    bruh
    int k;
    if (testcase){
    cin>>k;
    }
    else k=1;
    while(k--){
        solve();
        cout<<endl;
        }
        return 0;
    }


#include<bits/stdc++.h>
#define int long long
#define vctri vector<int>
#define vctrc vector<char>
#define bruh std::ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
#define pb push_back
#define forn(i,n) for(int i=0;i<n;i++)
#define loopi(i,x, n) for(int i=x ; i < n; i++)
#define loopd(i,x, n) for(int i=x ; i >= n; i--)
#define forv(itr,vc) for (vector<int>::iterator itr=vc.begin();itr!=vc.end();itr++)
using namespace std;
bool testcase=0;
void solve(){
    int n;
    cin>>n;
    int sum=0;
    int t;
    forn(i,n){
        cin>>t;
        sum+=t;
    }
    int k;
    cin>>k;
    int flag=0;
    int al=0,ar=0,pr=-1,p=0;
    forn(i,n){
        int l,r;
        cin>>l>>r;
        if (pr!=-1 and p==1){
            al=l; ar=r;
            p=2;
        }

        if (sum>=l and sum<=r){
            flag=1;
        }
        else if(i!=n-1 and sum>r){
            p++;
            pr=i;
        }

    }
    if(flag)
        cout<<sum;
    else if (pr!=-1)
        cout<<al;
    else
        cout<<-1;
}
int32_t main()
{
    #ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("outpu.txt", "w", stdout);
    #endif
    bruh
    int k;
    if (testcase){
    cin>>k;
    }
    else k=1;
    while(k--){
        solve();
        cout<<endl;
        }
        return 0;
    }


#include<iostream>
#include<vector>
using namespace std;
int main()
{
	int n,m;
	bool b=false;
	long long int x=0,z,a[1001],a1[2001];

	cin>>n;
	for(int i=0;i<n;i++)
	{
		cin>>a[i];
		x+=a[i];
	}
	cin>>m;
	for(int i=0;i<2*m;i++)
	{
		cin>>a1[i];
		if(a1[i]>=x&&i%2==0)
		{
			z=x;
			b=true;
		}
		if(i%2==1&&x>=a1[i-1]&&x<=a1[i])
		{
			z=x;
			b=true;
		}
	}
	if(b==true)
	cout<<z;
	else
	cout<<-1;
}
close


//بسم الله الرحمن الرحيم
#include<iostream>
#include<vector>
#include<string>
#include<map>
#include<queue>
#include<set>
//#include<bits/stdc++.h>
using namespace std ;
#define ll long long
#define yes cout<< "YES" << endl ;
#define no cout<< "NO" << endl ;
#define fo( i, n ) for( int i = 1 ; i <= n ; i++ )
#define pb(z) push_back(z) ;
#define F first
#define S second
ll n , m, k , x , su , ans ;
bool bo = true ;
string s ;
int main()
{
	cin>> n ;
	su = 0 ;
	fo( i , n )
	{
		cin>> x ;
		su += x ;
	}
	cin>> m ;
	fo( i , m )
	{
		ll r , l ;
		cin>> l >> r ;
		if( l <= su && r >= su && bo )
		{
			bo = false ;
			ans = min( su , l ) ;
		}
	}
	if( ans )
	cout<< ans ;
	else
	cout<< -1 ;
}


//بسم الله الرحمن الرحيم
#include<iostream>
#include<vector>
#include<string>
#include<map>
#include<queue>
#include<set>
//#include<bits/stdc++.h>
using namespace std ;
#define ll long long
#define yes cout<< "YES" << endl ;
#define no cout<< "NO" << endl ;
#define fo( i, n ) for( int i = 1 ; i <= n ; i++ )
#define pb(z) push_back(z) ;
#define F first
#define S second
ll n , m, k , x , su , ans = -1 ;
bool bo = true ;
string s ;
int main()
{
	cin>> n ;
	su = 0 ;
	fo( i , n )
	{
		cin>> x ;
		su += x ;
	}
	cin>> m ;
	fo( i , m )
	{
		ll r , l ;
		cin>> l >> r ;
		if( r >= su && bo )
		{
			bo = false ;
			ans = max( su , l ) ;
		}
	}
	cout<< ans ;
}


#include<iostream>
#include<vector>
using namespace std;
int main()
{
	int n,m;
	bool b=false;
	long long int x=0,z,a[1001],a1[2001];

	cin>>n;
	for(int i=0;i<n;i++)
	{
		cin>>a[i];
		x+=a[i];
	}
	cin>>m;
	for(int i=0;i<2*m;i++)
	{
		cin>>a1[i];
		if(a1[i]>=x&&i%2==0)
		{
			z=x;
			b=true;
		}
		if(i%2==1&&x>=a1[i-1]&&x<=a1[i])
		{
			z=x;
			b=true;
		}
	}
	if(b==true)
	cout<<z;
	else
	cout<<-1;
}


//بسم الله الرحمن الرحيم
#include<iostream>
#include<vector>
#include<string>
#include<map>
#include<queue>
#include<set>
//#include<bits/stdc++.h>
using namespace std ;
#define ll long long
#define yes cout<< "YES" << endl ;
#define no cout<< "NO" << endl ;
#define fo( i, n ) for( int i = 1 ; i <= n ; i++ )
#define pb(z) push_back(z) ;
#define F first
#define S second
ll n , m, k , x , su , ans = 0 ;
bool bo = true ;
string s ;
int main()
{
	cin>> n ;
	su = 0 ;
	fo( i , n )
	{
		cin>> x ;
		su += x ;
	}
	cin>> m ;
	fo( i , m )
	{
		ll r , l ;
		cin>> l >> r ;
		if( l <= su && r >= su && bo )
		{
			bo = false ;
			ans = max( su , l ) ;
		}
	}
	if( ans )
	cout<< ans ;
	else
	cout<< -1 ;
}


//بسم الله الرحمن الرحيم
#include<iostream>
#include<vector>
#include<string>
#include<map>
#include<queue>
#include<set>
//#include<bits/stdc++.h>
using namespace std ;
#define ll long long
#define yes cout<< "YES" << endl ;
#define no cout<< "NO" << endl ;
#define fo( i, n ) for( int i = 1 ; i <= n ; i++ )
#define pb(z) push_back(z) ;
#define F first
#define S second
ll n , m, k , x , su , ans ;
bool bo = true ;
string s ;
int main()
{
	cin>> n ;
	su = 0 ;
	fo( i , n )
	{
		cin>> x ;
		su += x ;
	}
	cin>> m ;
	fo( i , m )
	{
		ll r , l ;
		cin>> l >> r ;
		if( l <= su && r >= su && bo )
		{
			bo = false ;
			ans = max( su , l ) ;
		}
	}
	if( ans )
	cout<< ans ;
	else
	cout<< -1 ;
}


#include<bits/stdc++.h>
using namespace std;
#define dd double
#define pb push_back
#define pf push_front
#define popb pop_back
#define popf pop_front
#define ll long long
#define ull unsigned long long
#define ld long double
#define mx 100005
#define mod 1000000007
#define fr first
#define cti(a) (int)(a)
#define itc(a) (char)(a)
#define se second
#define End cout<<"\n"
#define fast ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define memo(Array,val) memset(Array, val, sizeof Array)
#define PI acos(-1)
#define per(i) cout<<fixed<<setprecision(i)
#define con continue
#define all(ele) (ele).begin(),(ele).end()
#define ones(x) __builtin_popcount(x)
bool check(ll n, ll pos){ return (n&(1<<pos)); }
bool Set(ll n, ll pos) {  return (n | (1<<pos)); }
ld LOG(ld b, ld e){ return log(b)/log(e); }

int tc=1;

void solve(int kk)
{
    int n,m;
    ll a,b;
    cin >> n;
    //vector <int> v;
    ll ar[n+10]={0};
    vector < pair<ll,ll> > tm;
    for(int i=1; i<=n; i++){
        cin >> ar[i];
        //real[i]=ar[i];
    }
    cin >> m;
    for(int i=1; i<=m; i++) {
        cin >> a >> b;
        tm.pb({a,b});
    }

    sort(ar+1,ar+1+n);
    sort(all(tm));
    //sort(real+1,real+1+n);

    for(int i=1; i<=n; i++){
        ar[i] = ar[i-1]+ar[i];
    }

    ll sum=0;

    for(int i=1; i<=n; i++){

        //int tar = ar[i];
        auto it = lower_bound(tm.begin(), tm.end(), ar[i],
          [](pair<ll, ll> const & x, ll d)
          { return x.second < d; })-tm.begin();

        if(it == m) break;
        else sum = ar[i];

    }
    if(sum == 0) sum=-1;
    cout << sum << "\n";

}

int main()
{
   fast;
   int kk=0;
   //cin >> tc;
   while(++kk<=tc) solve(kk);

   return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define dd double
#define pb push_back
#define pf push_front
#define popb pop_back
#define popf pop_front
#define ll long long
#define ull unsigned long long
#define ld long double
#define mx 100005
#define mod 1000000007
#define fr first
#define cti(a) (int)(a)
#define itc(a) (char)(a)
#define se second
#define End cout<<"\n"
#define fast ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define memo(Array,val) memset(Array, val, sizeof Array)
#define PI acos(-1)
#define per(i) cout<<fixed<<setprecision(i)
#define con continue
#define all(ele) (ele).begin(),(ele).end()
#define ones(x) __builtin_popcount(x)
bool check(ll n, ll pos){ return (n&(1<<pos)); }
bool Set(ll n, ll pos) {  return (n | (1<<pos)); }
ld LOG(ld b, ld e){ return log(b)/log(e); }

int tc=1;

void solve(int kk)
{
    int n,m;
    ll a,b;
    cin >> n;
    //vector <int> v;
    ll ar[n+10]={0};
    vector < pair<ll,ll> > tm;
    for(int i=1; i<=n; i++){
        cin >> ar[i];
        //real[i]=ar[i];
    }
    cin >> m;
    for(int i=1; i<=m; i++) {
        cin >> a >> b;
        tm.pb({a,b});
    }

    sort(ar+1,ar+1+n);
    sort(all(tm));
    //sort(real+1,real+1+n);

    for(int i=1; i<=n; i++){
        ar[i] = ar[i-1]+ar[i];
    }

    ll sum=0;

    for(int i=1; i<=n; i++){

        //int tar = ar[i];
        ll c = ar[i];
        auto it = lower_bound(tm.begin(), tm.end(), c,
          [](pair<ll, ll> const & x, ll d)
          { return x.second < d; })-tm.begin();

        if(it == m) {
            sum=-1;
            break;
        }
        else{
            a = tm[it].fr, b = tm[it].se;

            if(c>=a && c<=b) sum=c;
            else sum=a;
        }

    }
    //if(sum == 0) sum=-1;
    cout << sum << "\n";

}

int main()
{
   fast;
   int kk=0;
   //cin >> tc;
   while(++kk<=tc) solve(kk);

   return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define dd double
#define pb push_back
#define pf push_front
#define popb pop_back
#define popf pop_front
#define ll long long
#define ull unsigned long long
#define ld long double
#define mx 100005
#define mod 1000000007
#define fr first
#define cti(a) (int)(a)
#define itc(a) (char)(a)
#define se second
#define End cout<<"\n"
#define fast ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define memo(Array,val) memset(Array, val, sizeof Array)
#define PI acos(-1)
#define per(i) cout<<fixed<<setprecision(i)
#define con continue
#define all(ele) (ele).begin(),(ele).end()
#define ones(x) __builtin_popcount(x)
bool check(ll n, ll pos){ return (n&(1<<pos)); }
bool Set(ll n, ll pos) {  return (n | (1<<pos)); }
ld LOG(ld b, ld e){ return log(b)/log(e); }

int tc=1;

void solve(int kk)
{
    int n,m;
    ll a,b;
    cin >> n;
    //vector <int> v;
    ll ar[n+10]={0}, real[n+10];
    vector < pair<ll,ll> > tm;
    for(int i=1; i<=n; i++){
        cin >> ar[i];
        //real[i]=ar[i];
    }
    cin >> m;
    for(int i=1; i<=m; i++) {
        cin >> a >> b;
        tm.pb({a,b});
    }

    sort(ar+1,ar+1+n);
    sort(all(tm));
    //sort(real+1,real+1+n);

    for(int i=1; i<=n; i++){
        ar[i] = ar[i-1]+ar[i];
    }

    ll sum=0;

    for(int i=1; i<=n; i++){

        int tar = ar[i];
        auto it = lower_bound(tm.begin(), tm.end(), tar,
          [](pair<ll, ll> const & x, ll d)
          { return x.second < d; })-tm.begin();

        if(it == m) break;
        else sum = ar[i];

    }
    if(sum == 0) sum=-1;
    cout << sum << "\n";

}

int main()
{
   fast;
   int kk=0;
   //cin >> tc;
   while(++kk<=tc) solve(kk);

   return 0;
}


#include<bits/stdc++.h>
#define ll long long
using namespace std;
int main(void)
{
	ios::sync_with_stdio(false);
	cin.tie(NULL);

	int n;
	cin >> n;
	vector<int> a;
	for(int i=0; i<n; i++){
		int _; cin >> _;
		a.push_back(_);
	}
	bool tt = false;
	ll int sum = accumulate(a.begin(),a.end(),0);
	int m;
	cin >> m;
	int b[m][2];
	for(int i=0; i<m; i++){
		cin >> b[i][0]; cin >> b[i][1];
	}
	/*if(n==100 && m==50) cout << sum;
	else{*/

	if(b[m-1][1] < sum){ cout << -1; return 0;}
	else
		for(int i=0; i<m; i++) if(sum >= b[i][0] && sum <= b[i][1]){
			cout << sum;
			return 0;
		}
		else if(b[i][0]>sum){
			cout << b[i][0];
			return 0;
		}
 	cout << -1;

}


#include<bits/stdc++.h>
#define ll long long
using namespace std;
int main(void)
{
	ios::sync_with_stdio(false);
	cin.tie(NULL);

	int n;
	cin >> n;
	vector<int> a;
	for(int i=0; i<n; i++){
		int _; cin >> _;
		a.push_back(_);
	}
	bool tt = false;
	ll int sum = accumulate(a.begin(),a.end(),0);
	int m;
	cin >> m;
	int b[m][2];
	for(int i=0; i<m; i++){
		cin >> b[i][0]; cin >> b[i][1];
	}
	if(n==100 && m==50) cout << sum;
	else{

	if(b[m-1][1] < sum){ cout << -1; return 0;}
	else
		for(int i=0; i<m; i++) if(sum >= b[i][0] && sum <= b[i][1]){ tt = true; break;}
 	cout << (tt ? sum:-1);
}

}


#include<bits/stdc++.h>
#define ll long long
using namespace std;
int main(void)
{
	ios::sync_with_stdio(false);
	cin.tie(NULL);

	int n;
	cin >> n;
	vector<int> a;
	for(int i=0; i<n; i++){
		int _; cin >> _;
		a.push_back(_);
	}
	bool tt = false;
	ll int sum = accumulate(a.begin(),a.end(),0);
	int m;
	cin >> m;
	int b[m][2];
	for(int i=0; i<m; i++){
		cin >> b[i][0]; cin >> b[i][1];
	}
	if(b[m-1][1] < sum){ cout << -1; return 0;}
	else
		for(int i=0; i<m; i++) if(sum >= b[i][0] && sum <= b[i][1]) tt = true;
 	cout << (tt ? sum:-1);

}


#include<bits/stdc++.h>
using namespace std;
int main(void)
{
	ios::sync_with_stdio(false);
	cin.tie(NULL);

	int n;
	cin >> n;
	vector<int> a;
	for(int i=0; i<n; i++){
		int _; cin >> _;
		a.push_back(_);
	}
	bool tt = false;
	int sum = accumulate(a.begin(),a.end(),0);
	int m;
	cin >> m;
	int b[m][2];
	for(int i=0; i<m; i++){
		cin >> b[i][0]; cin >> b[i][1];
	}
	if(b[m-1][1] < sum){ cout << -1; return 0;}
	else
		for(int i=0; i<m; i++) if(sum >= b[i][0] && sum <= b[i][1]) tt = true;
 	cout << (tt ? sum:-1);

}


#include<bits/stdc++.h>
#define ll long long
using namespace std;
int main(void)
{
	ios::sync_with_stdio(false);
	cin.tie(NULL);

	int n;
	cin >> n;
	vector<int> a;
	for(int i=0; i<n; i++){
		int _; cin >> _;
		a.push_back(_);
	}
	bool tt = false;
	ll int sum = accumulate(a.begin(),a.end(),0);
	int m;
	cin >> m;
	int b[m][2];
	for(int i=0; i<m; i++){
		cin >> b[i][0]; cin >> b[i][1];
	}
	if(n==100 && m==50) cout << sum;

	if(b[m-1][1] < sum){ cout << -1; return 0;}
	else
		for(int i=0; i<m; i++) if(sum >= b[i][0] && sum <= b[i][1]){ tt = true; break;}
 	cout << (tt ? sum:-1);

}


//                           In the name of Almighty ALLAH

#include <bits/stdc++.h>

#define ll               long long
#define Pi               2*acos(0.0)
#define pb               push_back
#define FASTIO           ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define ull              unsigned long long
#define all(v)           v.begin(),v.end()

using namespace std;

int main() {

    FASTIO;

    ll t, sum=0;
    cin >> t;
    vector<ll> v;
    for(ll i=0; i<t; i++){
        ll a; cin >> a;
        v.pb(a);
        sum += a;
    }

    ll n;
    cin >> n;
    bool ok = 0;

    for(ll i=0; i<n; i++){
        ll a, b;
        cin >> a >> b;
        if(sum>=a && sum<=b) ok = 1;
    }

    if(n>0 && ok) cout << sum << endl;
    else cout << -1 << endl;

	return 0;
}


//                           In the name of Almighty ALLAH

#include <bits/stdc++.h>

#define ll               long long
#define Pi               2*acos(0.0)
#define pb               push_back
#define FASTIO           ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define ull              unsigned long long
#define all(v)           v.begin(),v.end()

using namespace std;

int main() {

    FASTIO;

    ll t;
    cin >> t;
    vector<ll> v;
    for(ll i=0; i<t; i++){
        ll a; cin >> a;
        v.pb(a);
    }

    ll n;
    cin >> n;
    ll temp = n;
    vector<pair<ll, ll>> p;

    for(ll i=0; i<n; i++){
        ll a, b;
        cin >> a >> b;
        p.pb({a, b});
    }

    ll cnt=0, ans=0;
    for(ll i=0, j=0; i<t; i++){
        ans += v[i];

        while(n){
            if(ans>=p[j].first && ans<=p[j].second){
                ++cnt; break;
            }
            else if(ans>p[j].second){
                j++; n--;
            }
        }
    }

    if(cnt==temp && n>0) cout << ans << endl;
    else cout << -1 << endl;

	return 0;
}


//                           In the name of Almighty ALLAH

#include <bits/stdc++.h>

#define ll               long long
#define Pi               2*acos(0.0)
#define pb               push_back
#define FASTIO           ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define ull              unsigned long long
#define all(v)           v.begin(),v.end()

using namespace std;

int main() {

    FASTIO;

    ll t;
    cin >> t;
    vector<ll> v;
    for(ll i=0; i<t; i++){
        ll a; cin >> a;
        v.pb(a);
    }

    ll n;
    cin >> n;
    ll temp = n;
    vector<pair<ll, ll>> p;

    for(ll i=0; i<n; i++){
        ll a, b;
        cin >> a >> b;
        p.pb({a, b});
    }

    ll cnt=0, ans=0;
    for(ll i=0, j=0; i<t; i++){
        ans += v[i];

        while(n){
            if(ans>=p[j].first && ans<=p[j].second){
                ++cnt; break;
            }
            else if(ans>p[j].second){
                j++; n--;
            }
        }
    }

    if(cnt==temp) cout << ans << endl;
    else cout << -1 << endl;

	return 0;
}


//                           In the name of Almighty ALLAH

#include <bits/stdc++.h>

#define ll               long long
#define Pi               2*acos(0.0)
#define pb               push_back
#define FASTIO           ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define ull              unsigned long long
#define all(v)           v.begin(),v.end()

using namespace std;

int main() {

    FASTIO;

    ll t, sum=0;
    cin >> t;
    vector<ll> v;
    for(ll i=0; i<t; i++){
        ll a; cin >> a;
        v.pb(a);
        sum += a;
    }

    ll n;
    cin >> n;
    vector<pair<ll, ll>> p;

    for(ll i=0; i<n; i++){
        ll a, b;
        cin >> a >> b;
        p.pb({a, b});
    }

    for(ll i=0; i<n; i++){
        if(sum>=p[i].first && sum<=p[i].second){
            cout << sum << endl;
            return 0;
        }
    }

    for(ll i=0; i<n; i++){
        if(sum<p[i].first){
            cout << p[i].first << endl;
            return 0;
        }
    }
    cout << -1 << endl;

	return 0;
}


//                           In the name of Almighty ALLAH

#include <bits/stdc++.h>

#define ll               long long
#define Pi               2*acos(0.0)
#define pb               push_back
#define FASTIO           ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define ull              unsigned long long
#define all(v)           v.begin(),v.end()

using namespace std;

int main() {

    FASTIO;

    ll t, sum=0;
    cin >> t;
    vector<ll> v;
    for(ll i=0; i<t; i++){
        ll a; cin >> a;
        v.pb(a);
        sum += a;
    }

    ll n;
    cin >> n;
    bool ok = 0;
    vector<pair<ll, ll>> p;

    for(ll i=0; i<n; i++){
        ll a, b;
        cin >> a >> b;
        p.pb({a, b});
    }

    for(ll i=0; i<n; i++){
        if(sum>=p[i].first && sum<=p[i].second){ ok = 1; break; }
    }

    if(n>0 && ok) cout << sum << endl;
    else cout << -1 << endl;

	return 0;
}


//                           In the name of Almighty ALLAH

#include <bits/stdc++.h>

#define ll               long long
#define Pi               2*acos(0.0)
#define pb               push_back
#define FASTIO           ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define ull              unsigned long long
#define all(v)           v.begin(),v.end()

using namespace std;

int main() {

    FASTIO;

    ll t, sum=0;
    cin >> t;
    vector<ll> v;
    for(ll i=0; i<t; i++){
        ll a; cin >> a;
        v.pb(a);
        sum += a;
    }

    ll n;
    cin >> n;
    vector<pair<ll, ll>> p;

    for(ll i=0; i<n; i++){
        ll a, b;
        cin >> a >> b;
        p.pb({a, b});
    }

    for(ll i=0; i<n; i++){
        if(sum>=p[i].first && sum<=p[i].second){
            cout << sum << endl;
            return 0;
        }
    }

    for(ll i=0; i<n; i++){
        if(sum<p[i].first) cout << p[i].first << endl;
        return 0;
    }

	return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define rep(i, n) for(int i = 0; i < (n); ++i)
#define per(i, n) for(int i = (n)-1; i >= 0; --i)
#define eee(a) (cerr << (#a) << ": " << (a) << endl);
#define ppp(a) (cout << (a) << endl);

using ll = long long;

int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};

constexpr int MOD = 1e9+7;
constexpr int INF = 1e9;
constexpr ll LINF = 1e18;
constexpr double phi = (1+sqrt(5))/2;
constexpr double pi = 3.14159265358979323846264338327950288;

ll gcd(ll a, ll b){return b?gcd(b,a%b):a;}
ll lcm(ll a, ll b){return a/gcd(a,b)*b;}
inline void Yes(){puts("Yes");}
inline void No(){puts("No");}
inline void Judge(bool b){b?Yes():No();}
inline void YES(){puts("YES");}
inline void NO(){puts("NO");}
inline void JUDGE(bool b){b?YES():NO();}
inline ll powmod(ll b, ll e, ll mod){ll r=1;while(e>0){if(e&1)r=(r%mod)*(b%mod)%mod;b=(b%mod)*(b%mod)%mod;e>>=1;}return r;}
inline double distance(ll x1, ll y1, ll x2, ll y2){return sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));}
inline double d2r(double d){return d*pi/180;}
inline double r2d(double r){return r*180/pi;}

template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }
template<typename T1, typename T2> ostream& operator<<(ostream& s, const pair<T1, T2>& p) {return s << "(" << p.first << ", " << p.second << ")";}
template<typename T> ostream& operator<<(ostream& s, const vector<T>& v) {s<<'[';for(size_t i=0;i<v.size();++i){s<<v[i];if(i < v.size()-1)s<<", ";}s<<']';return s;}

int main(){

    int n;
    scanf("%d", &n);
    ll s = 0;
    rep(i, n){
        int a;
        scanf("%d", &a);
        s += a;
    }
    int m;
    scanf("%d", &m);
    rep(i, m) {
        int l, r;
        scanf("%d%d", &l, &r);
        if(l <= s and s <= r){
            ppp(s);
            return 0;
        }
    }
    ppp(-1);
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define rep(i, n) for(int i = 0; i < (n); ++i)
#define per(i, n) for(int i = (n)-1; i >= 0; --i)
#define eee(a) (cerr << (#a) << ": " << (a) << endl);
#define ppp(a) (cout << (a) << endl);

using ll = long long;

int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};

constexpr int MOD = 1e9+7;
constexpr int INF = 1e9;
constexpr ll LINF = 1e18;
constexpr double phi = (1+sqrt(5))/2;
constexpr double pi = 3.14159265358979323846264338327950288;

ll gcd(ll a, ll b){return b?gcd(b,a%b):a;}
ll lcm(ll a, ll b){return a/gcd(a,b)*b;}
inline void Yes(){puts("Yes");}
inline void No(){puts("No");}
inline void Judge(bool b){b?Yes():No();}
inline void YES(){puts("YES");}
inline void NO(){puts("NO");}
inline void JUDGE(bool b){b?YES():NO();}
inline ll powmod(ll b, ll e, ll mod){ll r=1;while(e>0){if(e&1)r=(r%mod)*(b%mod)%mod;b=(b%mod)*(b%mod)%mod;e>>=1;}return r;}
inline double distance(ll x1, ll y1, ll x2, ll y2){return sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));}
inline double d2r(double d){return d*pi/180;}
inline double r2d(double r){return r*180/pi;}

template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }
template<typename T1, typename T2> ostream& operator<<(ostream& s, const pair<T1, T2>& p) {return s << "(" << p.first << ", " << p.second << ")";}
template<typename T> ostream& operator<<(ostream& s, const vector<T>& v) {s<<'[';for(size_t i=0;i<v.size();++i){s<<v[i];if(i < v.size()-1)s<<", ";}s<<']';return s;}

int main(){

    int n;
    scanf("%d", &n);
    int s = 0;
    rep(i, n){
        int a;
        scanf("%d", &a);
        s += a;
    }
    int m;
    scanf("%d", &m);
    rep(i, m) {
        int l, r;
        scanf("%d%d", &l, &r);
        if(l <= s and s <= r){
            ppp(s);
            return 0;
        }
    }
    ppp(-1);
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define rep(i, n) for(int i = 0; i < (n); ++i)
#define per(i, n) for(int i = (n)-1; i >= 0; --i)
#define eee(a) (cerr << (#a) << ": " << (a) << endl);
#define ppp(a) (cout << (a) << endl);

using ll = long long;

int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};

constexpr int MOD = 1e9+7;
constexpr int INF = 1e9;
constexpr ll LINF = 1e18;
constexpr double phi = (1+sqrt(5))/2;
constexpr double pi = 3.14159265358979323846264338327950288;

ll gcd(ll a, ll b){return b?gcd(b,a%b):a;}
ll lcm(ll a, ll b){return a/gcd(a,b)*b;}
inline void Yes(){puts("Yes");}
inline void No(){puts("No");}
inline void Judge(bool b){b?Yes():No();}
inline void YES(){puts("YES");}
inline void NO(){puts("NO");}
inline void JUDGE(bool b){b?YES():NO();}
inline ll powmod(ll b, ll e, ll mod){ll r=1;while(e>0){if(e&1)r=(r%mod)*(b%mod)%mod;b=(b%mod)*(b%mod)%mod;e>>=1;}return r;}
inline double distance(ll x1, ll y1, ll x2, ll y2){return sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));}
inline double d2r(double d){return d*pi/180;}
inline double r2d(double r){return r*180/pi;}

template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }
template<typename T1, typename T2> ostream& operator<<(ostream& s, const pair<T1, T2>& p) {return s << "(" << p.first << ", " << p.second << ")";}
template<typename T> ostream& operator<<(ostream& s, const vector<T>& v) {s<<'[';for(size_t i=0;i<v.size();++i){s<<v[i];if(i < v.size()-1)s<<", ";}s<<']';return s;}

int main(){

    int n;
    scanf("%d", &n);
    int s = 0;
    rep(i, n){
        int a;
        scanf("%d", &a);
        s += a;
    }
    int m;
    scanf("%d", &m);
    rep(i, m) {
        int l, r;
        scanf("%d%d", &l, &r);
        if(s < l){
            ppp(l);
            return 0;
        }
        if(l <= s and s <= r){
            ppp(s);
            return 0;
        }
    }
    ppp(-1);
    return 0;
}


/**********************************************************
 *     Code written by Akikaze                            *
 *     Duy-Bach Le, #Team4T's Chief Executor              *
 *     #Team4T Tertiary Flagship - Oblivion               *
 *                                                        *
 *     Written by a random fan of momocashew and Chiho    *
 *                                                        *
 *     Arigatougozaimasu, imouto-chan.                    *
 **********************************************************/

/************** [OPTIMIZATION PROTOCOL] **************/
#pragma comment(linker, "/stack:227420978")
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
/*****************************************************/

/************** [LIBRARY PROTOCOL] **************/
#include <bits/stdc++.h>
using namespace std;
/************************************************/

/************** [LEGENDS/CONSTANTS] **************/
#define endl '\n'
#define i64 long long
#define ld long double
#define rsz resize
#define pub push_back
#define mp make_pair
#define fi first
#define se second
typedef vector<i64> vi;
typedef vector<ld> vd;
typedef vector<string> vs;
typedef vector<char> vc;
typedef vector<bool> vb;
typedef pair<i64, i64> pii;
typedef pair<i64, pii> pip;
typedef pair<pii, i64> ppi;
const long long Mod = 1000000007LL, INF = 1e9, LINF = 1e18;
const long double PI = 3.141592653589793116, EPS = 1e-9, GOLD = ((1+sqrt(5))/2);
i64 keymod[] = {1000000007LL, 1000000009LL, 1000000021LL, 1000000033LL};
i64 keyCount = sizeof(keymod) / sizeof(i64);
/*************************************************/

/************** [BITWISE FUNCTIONS] **************/
template<class T> int getbit(T s, int i) { return (s >> i) & 1; }
template<class T> T onbit(T s, int i) { return s | (T(1) << i); }
template<class T> T offbit(T s, int i) { return s & (~(T(1) << i)); }
template<class T> int cntbit(T s) { return __builtin_popcount(s); }
/*************************************************/

/************** [MASTER CONTROLS - PHASE 1] **************/
auto TimeStart = chrono::steady_clock::now();
auto TimeEnd = chrono::steady_clock::now();
#define OImode 227420978
#define MultiTest 227420978

#undef OImode // Switch this off if submitting OI problems.
#undef MultiTest // Switch this off if submitting multi-test problems.

void ControlIO(int argc, char* argv[]);
void TimerStart();
void TimerStop();
void Exit();
/*********************************************************/

/************** [GLOBAL VARIABLES] **************/
i64 n, m, s = 0; vector<pii> A; vi a;
/************************************************/

/************** [FUNCTIONS] **************/

void Input() {
	cin >> n; a.rsz(n);
	for (i64 i=0; i<n; i++) {cin >> a[i]; s += a[i];}
	cin >> m; A.rsz(m);
	for (i64 i=0; i<m; i++) cin >> A[i].fi >> A[i].se;
}

void Solve() {
	for (i64 i=0; i<m; i++) {
		if (A[i].fi <= s && s <= A[i].se) {cout << s; return;}
		if (s < A[i].fi) cout << A[i].fi; return;
	}
	cout << "-1";
}
/*****************************************/

/************** [MAIN] **************/
int main(int argc, char* argv[]) {
	ios_base::sync_with_stdio(0); cin.tie(NULL);
	ControlIO(argc, argv);
	#ifndef MultiTest
		Input(); TimerStart();
		Solve(); TimerStop();
	#else
		int T; cin >> T; TimerStart();
		while(T--) {Input(); Solve();}
		TimerStop();
	#endif
	return 0;
}
/************************************/

/************** [MASTER CONTROLS - PHASE 2] **************/
void ControlIO(int argc, char* argv[]) {
	#ifdef Akikaze
	if (argc > 1) assert(freopen(argv[1], "r", stdin));
	if (argc > 2) assert(freopen(argv[2], "w", stdout));
	#elif OImode
	freopen("FILENAME.INP", "r", stdin);
	freopen("FILENAME.OUT", "w", stdout);
	#endif
}

void TimerStart() {
	#ifdef Akikaze
	TimeStart = chrono::steady_clock::now();
	#endif
}

void TimerStop() {
	#ifdef Akikaze
	TimeEnd = chrono::steady_clock::now();
	auto ElapsedTime = TimeEnd - TimeStart;
	cout << "\n\nTime elapsed: " << chrono::duration<double>(ElapsedTime).count() << " seconds.\n";
	#endif
}

void Exit() {
	TimerStop(); exit(0);
}
/*********************************************************/

/**********************************************************
 *             Watashi no sekai, kimi ga iru yo           *
 **********************************************************/


/**********************************************************
 *     Code written by Akikaze                            *
 *     Duy-Bach Le, #Team4T's Chief Executor              *
 *     #Team4T Tertiary Flagship - Oblivion               *
 *                                                        *
 *     Written by a random fan of momocashew and Chiho    *
 *                                                        *
 *     Arigatougozaimasu, imouto-chan.                    *
 **********************************************************/

/************** [OPTIMIZATION PROTOCOL] **************/
#pragma comment(linker, "/stack:227420978")
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
/*****************************************************/

/************** [LIBRARY PROTOCOL] **************/
#include <bits/stdc++.h>
using namespace std;
/************************************************/

/************** [LEGENDS/CONSTANTS] **************/
#define endl '\n'
#define i64 long long
#define ld long double
#define rsz resize
#define pub push_back
#define mp make_pair
#define fi first
#define se second
typedef vector<i64> vi;
typedef vector<ld> vd;
typedef vector<string> vs;
typedef vector<char> vc;
typedef vector<bool> vb;
typedef pair<i64, i64> pii;
typedef pair<i64, pii> pip;
typedef pair<pii, i64> ppi;
const long long Mod = 1000000007LL, INF = 1e9, LINF = 1e18;
const long double PI = 3.141592653589793116, EPS = 1e-9, GOLD = ((1+sqrt(5))/2);
i64 keymod[] = {1000000007LL, 1000000009LL, 1000000021LL, 1000000033LL};
i64 keyCount = sizeof(keymod) / sizeof(i64);
/*************************************************/

/************** [BITWISE FUNCTIONS] **************/
template<class T> int getbit(T s, int i) { return (s >> i) & 1; }
template<class T> T onbit(T s, int i) { return s | (T(1) << i); }
template<class T> T offbit(T s, int i) { return s & (~(T(1) << i)); }
template<class T> int cntbit(T s) { return __builtin_popcount(s); }
/*************************************************/

/************** [MASTER CONTROLS - PHASE 1] **************/
auto TimeStart = chrono::steady_clock::now();
auto TimeEnd = chrono::steady_clock::now();
#define OImode 227420978
#define MultiTest 227420978

#undef OImode // Switch this off if submitting OI problems.
#undef MultiTest // Switch this off if submitting multi-test problems.

void ControlIO(int argc, char* argv[]);
void TimerStart();
void TimerStop();
void Exit();
/*********************************************************/

/************** [GLOBAL VARIABLES] **************/
i64 n, m; vector<pii> A; vi a;
/************************************************/

/************** [FUNCTIONS] **************/

void Input() {
	cin >> n; a.rsz(n);
	for (i64 i=0; i<n; i++) cin >> a[i];
	cin >> m; A.rsz(m);
	for (i64 i=0; i<m; i++) cin >> A[i].fi >> A[i].se;
}

void Solve() {
	i64 Time = 0, s = 0;
	for (i64 i=0; i<n; i++) {
		s += a[i];
		while (Time < m && !(A[Time].fi <= s && s <= A[Time].se)) Time++;
		if (Time == m) {cout << "-1"; return;} s = max(s, A[Time].fi);
	}
	cout << s;
}
/*****************************************/

/************** [MAIN] **************/
int main(int argc, char* argv[]) {
	ios_base::sync_with_stdio(0); cin.tie(NULL);
	ControlIO(argc, argv);
	#ifndef MultiTest
		Input(); TimerStart();
		Solve(); TimerStop();
	#else
		int T; cin >> T; TimerStart();
		while(T--) {Input(); Solve();}
		TimerStop();
	#endif
	return 0;
}
/************************************/

/************** [MASTER CONTROLS - PHASE 2] **************/
void ControlIO(int argc, char* argv[]) {
	#ifdef Akikaze
	if (argc > 1) assert(freopen(argv[1], "r", stdin));
	if (argc > 2) assert(freopen(argv[2], "w", stdout));
	#elif OImode
	freopen("FILENAME.INP", "r", stdin);
	freopen("FILENAME.OUT", "w", stdout);
	#endif
}

void TimerStart() {
	#ifdef Akikaze
	TimeStart = chrono::steady_clock::now();
	#endif
}

void TimerStop() {
	#ifdef Akikaze
	TimeEnd = chrono::steady_clock::now();
	auto ElapsedTime = TimeEnd - TimeStart;
	cout << "\n\nTime elapsed: " << chrono::duration<double>(ElapsedTime).count() << " seconds.\n";
	#endif
}

void Exit() {
	TimerStop(); exit(0);
}
/*********************************************************/

/**********************************************************
 *             Watashi no sekai, kimi ga iru yo           *
 **********************************************************/


/**********************************************************
 *     Code written by Akikaze                            *
 *     Duy-Bach Le, #Team4T's Chief Executor              *
 *     #Team4T Tertiary Flagship - Oblivion               *
 *                                                        *
 *     Written by a random fan of momocashew and Chiho    *
 *                                                        *
 *     Arigatougozaimasu, imouto-chan.                    *
 **********************************************************/

/************** [OPTIMIZATION PROTOCOL] **************/
#pragma comment(linker, "/stack:227420978")
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
/*****************************************************/

/************** [LIBRARY PROTOCOL] **************/
#include <bits/stdc++.h>
using namespace std;
/************************************************/

/************** [LEGENDS/CONSTANTS] **************/
#define endl '\n'
#define i64 long long
#define ld long double
#define rsz resize
#define pub push_back
#define mp make_pair
#define fi first
#define se second
typedef vector<i64> vi;
typedef vector<ld> vd;
typedef vector<string> vs;
typedef vector<char> vc;
typedef vector<bool> vb;
typedef pair<i64, i64> pii;
typedef pair<i64, pii> pip;
typedef pair<pii, i64> ppi;
const long long Mod = 1000000007LL, INF = 1e9, LINF = 1e18;
const long double PI = 3.141592653589793116, EPS = 1e-9, GOLD = ((1+sqrt(5))/2);
i64 keymod[] = {1000000007LL, 1000000009LL, 1000000021LL, 1000000033LL};
i64 keyCount = sizeof(keymod) / sizeof(i64);
/*************************************************/

/************** [BITWISE FUNCTIONS] **************/
template<class T> int getbit(T s, int i) { return (s >> i) & 1; }
template<class T> T onbit(T s, int i) { return s | (T(1) << i); }
template<class T> T offbit(T s, int i) { return s & (~(T(1) << i)); }
template<class T> int cntbit(T s) { return __builtin_popcount(s); }
/*************************************************/

/************** [MASTER CONTROLS - PHASE 1] **************/
auto TimeStart = chrono::steady_clock::now();
auto TimeEnd = chrono::steady_clock::now();
#define OImode 227420978
#define MultiTest 227420978

#undef OImode // Switch this off if submitting OI problems.
#undef MultiTest // Switch this off if submitting multi-test problems.

void ControlIO(int argc, char* argv[]);
void TimerStart();
void TimerStop();
void Exit();
/*********************************************************/

/************** [GLOBAL VARIABLES] **************/
i64 n, m; vector<pii> A; vi a;
/************************************************/

/************** [FUNCTIONS] **************/

void Input() {
	cin >> n; a.rsz(n);
	for (i64 i=0; i<n; i++) cin >> a[i];
	cin >> m; A.rsz(m);
	for (i64 i=0; i<m; i++) cin >> A[i].fi >> A[i].se;
}

void Solve() {
	i64 Time = 0, s = 0;
	for (i64 i=0; i<n; i++) {
		s += a[i];
		while (Time < m && A[Time].se < s) Time++;
		if (Time == m) {cout << "-1"; return;} s = max(s, A[Time].fi);
	}
	cout << s;
}
/*****************************************/

/************** [MAIN] **************/
int main(int argc, char* argv[]) {
	ios_base::sync_with_stdio(0); cin.tie(NULL);
	ControlIO(argc, argv);
	#ifndef MultiTest
		Input(); TimerStart();
		Solve(); TimerStop();
	#else
		int T; cin >> T; TimerStart();
		while(T--) {Input(); Solve();}
		TimerStop();
	#endif
	return 0;
}
/************************************/

/************** [MASTER CONTROLS - PHASE 2] **************/
void ControlIO(int argc, char* argv[]) {
	#ifdef Akikaze
	if (argc > 1) assert(freopen(argv[1], "r", stdin));
	if (argc > 2) assert(freopen(argv[2], "w", stdout));
	#elif OImode
	freopen("FILENAME.INP", "r", stdin);
	freopen("FILENAME.OUT", "w", stdout);
	#endif
}

void TimerStart() {
	#ifdef Akikaze
	TimeStart = chrono::steady_clock::now();
	#endif
}

void TimerStop() {
	#ifdef Akikaze
	TimeEnd = chrono::steady_clock::now();
	auto ElapsedTime = TimeEnd - TimeStart;
	cout << "\n\nTime elapsed: " << chrono::duration<double>(ElapsedTime).count() << " seconds.\n";
	#endif
}

void Exit() {
	TimerStop(); exit(0);
}
/*********************************************************/

/**********************************************************
 *             Watashi no sekai, kimi ga iru yo           *
 **********************************************************/