#include <bits/stdc++.h>
using namespace std;
int n, l, p, r;
int main() {
  for (cin >> n; n--;) cin >> l, p += l;
  for (cin >> n; n-- && p > r;) cin >> l >> r;
  cout << (p > r ? -1 : max(p, l));
}
