#include <bits/stdc++.h>
using namespace std;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  long long n;
  cin >> n;
  vector<long long> a(n);
  for (long long i = 0; i < n; i++) cin >> a[i];
  long long m;
  cin >> m;
  sort(a.begin(), a.end());
  long long sum = 0;
  long long count = 0;
  long long j = 0;
  for (long long i = 0; i < m; i++) {
    long long l, r;
    cin >> l >> r;
    int f = 0;
    while (sum <= r) {
      if (sum + a[j] <= r) {
        sum = sum + a[j];
        j++;
        count++;
      } else {
        break;
      }
      if (count == n) {
        sum = max(sum, l);
        f = 1;
        break;
      }
    }
    if (f) break;
  }
  if (count == n)
    cout << sum << endl;
  else
    cout << -1 << endl;
  return 0;
}
