Pasha is participating in a contest on one well-known website. This time he wants to win the contest and will do anything to get to the first place!

This contest consists of n problems, and Pasha solves ith problem in a_{i} time units (his solutions are always correct). At any moment of time he can be thinking about a solution to only one of the problems (that is, he cannot be solving two problems at the same time). The time Pasha spends to send his solutions is negligible. Pasha can send any number of solutions at the same moment.

Unfortunately, there are too many participants, and the website is not always working. Pasha received the information that the website will be working only during m time periods, jth period is represented by its starting moment l_{j} and ending moment r_{j}. Of course, Pasha can send his solution only when the website is working. In other words, Pasha can send his solution at some moment T iff there exists a period x such that l_{x} ≤ T ≤ r_{x}.

Pasha wants to know his best possible result. We need to tell him the minimal moment of time by which he is able to have solutions to all problems submitted, if he acts optimally, or say that it's impossible no matter how Pasha solves the problems.


-----Input-----

The first line contains one integer n (1 ≤ n ≤ 1000) — the number of problems. The second line contains n integers a_{i} (1 ≤ a_{i} ≤ 10^5) — the time Pasha needs to solve ith problem.

The third line contains one integer m (0 ≤ m ≤ 1000) — the number of periods of time when the website is working. Next m lines represent these periods. jth line contains two numbers l_{j} and r_{j} (1 ≤ l_{j} < r_{j} ≤ 10^5) — the starting and the ending moment of jth period.

It is guaranteed that the periods are not intersecting and are given in chronological order, so for every j > 1 the condition l_{j} > r_{j} - 1 is met.


-----Output-----

If Pasha can solve and submit all the problems before the end of the contest, print the minimal moment of time by which he can have all the solutions submitted.

Otherwise print "-1" (without brackets).


-----Examples-----
Input
2
3 4
2
1 4
7 9

Output
7

Input
1
5
1
1 4

Output
-1

Input
1
5
1
1 5

Output
5



-----Note-----

In the first example Pasha can act like this: he solves the second problem in 4 units of time and sends it immediately. Then he spends 3 time units to solve the first problem and sends it 7 time units after the contest starts, because at this moment the website starts working again.

In the second example Pasha invents the solution only after the website stops working for the last time.

In the third example Pasha sends the solution exactly at the end of the first period.

Source: codeforces | Difficulty: EASY
Tags: Implementation


Solutions:
n = int(input())
problem = sum([int(x) for x in input().split()])
for _ in range(int(input())):
	(l, r) = [int(x) for x in input().split()]
	if problem <= r:
		if problem <= l:
			print(l)
		else:
			print(problem)
		break
else:
	print(-1)


import sys
import bisect
from bisect import bisect_left as lb
input_ = lambda : sys.stdin.readline().strip('\r\n')
from math import log
from math import gcd
from math import atan2, acos
from random import randint
sa = lambda : input_()
sb = lambda : int(input_())
sc = lambda : input_().split()
sd = lambda : list(map(int, input_().split()))
sflo = lambda : list(map(float, input_().split()))
se = lambda : float(input_())
sf = lambda : list(input_())
flsh = lambda : sys.stdout.flush()
mod = 10 ** 9 + 7
mod1 = 998244353
gp = []
cost = []
dp = []
mx = []
ans1 = []
ans2 = []
special = []
specnode = []
a = 0
kthpar = []

def dfs(root, par):
	if par != -1:
		dp[root] = dp[par] + 1
	for i in range(1, 20):
		if kthpar[root][i - 1] != -1:
			kthpar[root][i] = kthpar[kthpar[root][i - 1]][i - 1]
	for child in gp[root]:
		if child == par:
			continue
		kthpar[child][0] = root
		dfs(child, root)
ans = 0
b = []

def dfs2(root, par):
	global gp, special, dp
	s = 1 if special[root] else 0
	for child in gp[root]:
		if child == par:
			continue
		s += dfs2(child, root)
	dp[root] = s
	return s

def hnbhai(tc):
	n = sb()
	a = sd()
	m = sb()
	p = []
	for i in range(m):
		p.append(sd())
	p.sort()
	x = sum(a)
	ss = 0
	for (i, j) in p:
		if x >= i and x <= j:
			print(x)
			return
		elif x <= i:
			print(i)
			return
	print(-1)
for _ in range(1):
	hnbhai(_ + 1)


def main():
	n = int(input())
	a = [int(s) for s in input().split()]
	m = int(input())
	if m == 0:
		print(-1)
		return
	(l, r) = ([], [])
	for _ in range(m):
		(x, y) = [int(s) for s in input().split()]
		l.append(x)
		r.append(y)
	total_time = sum(a)
	period = 0
	while period < m and total_time > r[period]:
		period += 1
	if period < m:
		print(max(total_time, l[period]))
	else:
		print(-1)
main()


n = int(input())
time = list(map(int, input().split()))
i = 0
s = 0
while i < len(time):
	s += time[i]
	i += 1
m = int(input())
flag = False
while m > 0:
	(l, r) = map(int, input().split())
	if l > s:
		print(l)
		flag = True
		break
	elif s >= l and s <= r:
		print(s)
		flag = True
		break
	m -= 1
if flag == False:
	print('-1')


n = int(input())
arr = [int(x) for x in input().split()]
total_time_needed = sum(arr)
num_intervals = int(input())
for i in range(num_intervals):
	(a, b) = [int(x) for x in input().split()]
	if b >= total_time_needed:
		print(max(a, total_time_needed))
		exit()
print(-1)


n = int(input())
tot = sum(map(int, input().split()))
m = int(input())
P = []
for i in range(m):
	(l, r) = list(map(int, input().split()))
	P.append((l, r))
for (l, r) in P:
	if l <= tot and tot <= r:
		print(tot)
		return
	elif tot <= l:
		print(l)
		return
print(-1)


n = int(input())
m = sum(map(int, input().split()))
t = 0
for _ in range(0, int(input())):
	(a, b) = map(int, input().split())
	if a >= m:
		print(a)
		t = 1
		break
	if a <= m and b >= m:
		print(m)
		t = 1
		break
if t == 0:
	print('-1')


N = int(input())
As = list(map(int, input().split()))
sumOfAs = sum(As)
M = int(input())
(maxL, maxR) = (0, 0)
isPossible = False
solution = -1
for m in range(M):
	(maxL, maxR) = list(map(int, input().split()))
	if not isPossible and sumOfAs <= maxR:
		isPossible = True
		solution = max(maxL, sumOfAs)
print(solution)


n = int(input())
sum = 0
tokens = input().split()
for tok in tokens:
	sum += int(tok)
m = int(input())
res = -1
for i in range(m):
	tokens = input().split()
	start = int(tokens[0])
	stop = int(tokens[1])
	if start <= sum and sum <= stop:
		res = sum
	elif start > sum and res == -1:
		res = start
print(res)


n = int(input())
t = sum(map(int, input().split()))
m = int(input())
for i in range(m):
	(l, r) = map(int, input().split())
	if l <= t <= r:
		print(t)
		quit()
	elif l >= t:
		print(l)
		quit()
print(-1)


n = int(input())
a = sum(map(int, input().split()))
for _ in range(int(input())):
	(q, w) = map(int, input().split())
	if a <= w:
		exit(print(max(a, q)))
print(-1)


n = int(input())
ai = list(map(int, input().split()))
q = int(input())
dur = []
for i in range(q):
	(l, r) = map(int, input().split())
	if sum(ai) <= r:
		print(max(sum(ai), l))
		exit(0)
print(-1)


n = int(input())
t = sum(map(int, input().split()))
m = int(input())
possible = False
best = 1000000000
for i in range(m):
	(l, r) = list(map(int, input().split()))
	if l <= t <= r:
		possible = True
	if l >= t:
		best = min(best, l)
if possible:
	print(t)
elif best < 1000000000:
	print(best)
else:
	print(-1)


n = int(input())
a = list(map(int, input().split()))
time = sum(a)
t = -1
m = int(input())
intervals = []
for i in range(m):
	(r, l) = list(map(int, input().split()))
	intervals.append((r, l))
for i in intervals:
	if time <= i[0]:
		t = i[0]
		break
	elif time <= i[1]:
		t = time
		break
print(t)


n = int(input())
a = list(map(int, input().split()))
su = sum(a)
m = int(input())
ans = -1
error = 0
for i in range(m):
	(s, f) = map(int, input().split())
	for j in range(s, f + 1):
		if su <= j and error == 0:
			ans = j
			error = 1
print(ans)


n = int(input())
s = sum(list(map(int, input().split())))
m = int(input())
for i in range(m):
	(l, r) = list(map(int, input().split()))
	s = max(s, l)
	if l <= s <= r:
		print(s)
		return
print(-1)


n = int(input())
sm = 0
for i in input().split():
	sm += int(i)
m = int(input())
for i in range(m):
	(a, b) = (int(j) for j in input().split())
	if sm <= b:
		if sm >= a:
			print(sm)
		else:
			print(a)
		sm = -1
		break
if sm != -1:
	print('-1')


import sys

def solve():
	n = int(sys.stdin.readline().rstrip())
	a = [int(i) for i in sys.stdin.readline().split()]
	tot = sum(a)
	m = int(sys.stdin.readline().rstrip())
	for i in range(m):
		(li, ri) = map(int, sys.stdin.readline().split())
		if tot < li:
			print(li)
			return
		elif li <= tot <= ri:
			print(tot)
			return
	print(-1)
solve()


import sys
n = int(input())
t = sum(map(int, input().split()))
inf = 10 ** 9
ans = inf
for _ in range(int(input())):
	(l, r) = map(int, input().split())
	if t <= r:
		ans = min(ans, max(l, t))
print(ans if ans != inf else -1)


n = int(input())
s = input()
l = s.split()
list = []
t = 0
while t < n:
	list.append(int(l[t]))
	t += 1
t = 0
sum = 0
while t < n:
	sum += list[t]
	t += 1
m = int(input())
i = 0
time = []
while i < m:
	s1 = input()
	l1 = s1.split()
	time.append(int(l1[0]))
	time.append(int(l1[1]))
	i += 1
k = 0
if m != 0:
	while time[k] < sum:
		k += 1
		if k >= 2 * m:
			print('-1')
			break
	if k % 2 == 1 and k < 2 * m:
		print(sum)
	if k % 2 == 0 and k < 2 * m:
		print(time[k])
if m == 0:
	print('-1')


n = int(input())
arr = list(map(int, input().split()))
m = int(input())
moment = []
my = 0
for i in range(m):
	(a, b) = map(int, input().split())
	moment.append((a, b))
	if b > my:
		my = b
moment = sorted(moment)
total = sum(arr)
if total <= my:
	for (a, b) in moment:
		if total <= b:
			print(str(max(total, a)))
			break
else:
	print('-1')


n = int(input())
a = list(map(int, input().split()))
m = int(input())
if m == 0:
	print(-1)
	exit()
s = sum(a)
r = 0
for i in range(m):
	inp = list(map(int, input().split()))
	if inp[0] <= s <= inp[1]:
		print(s)
		exit()
	elif r <= s <= inp[1]:
		print(inp[0])
		exit()
	else:
		r = inp[1]
if s > r:
	print(-1)


n = int(input())
lst = list(map(int, input().strip().split(' ')))
f = 0
c = 0
Sum = sum(lst)
m = int(input())
for j in range(m):
	(s, e) = map(int, input().strip().split(' '))
	if Sum >= s and Sum <= e:
		f = 1
		c = 2
		print(Sum)
		break
	elif c != 2 and Sum < s:
		f = 1
		print(s)
		break
if f == 0:
	print(-1)


n = int(input())
l = list(map(int, input().split()))
s = sum(l)
m = int(input())
imos = [0 for _ in range(101010)]
for _ in range(m):
	(a, b) = map(int, input().split())
	imos[a] += 1
	imos[b + 1] -= 1
for i in range(1, 101010):
	imos[i] += imos[i - 1]
	if imos[i] > 0 and i >= s:
		print(i)
		exit()
print(-1)


n = int(input())
s = 0
p = input().split()
for i in range(0, n):
	s += int(p[i])
m = int(input())
for i in range(0, m):
	p = input().split()
	l = int(p[0])
	r = int(p[1])
	if s >= l and s <= r:
		print(s)
		quit()
	elif s < l:
		print(l)
		quit()
print(-1)


n = int(input())
s = 0
for i in map(int, input().split()):
	s += i
m = int(input())
sol = -1
for i in range(m):
	d = list(map(int, input().split()))
	if sol == -1:
		if s >= d[0] and s <= d[1]:
			sol = s
		elif s <= d[0]:
			sol = d[0]
print(sol)


n = int(input())
a = [int(i) for i in input().split()]
m = int(input())
d = sum(a)
flag = 0
if m == 0:
	print(-1)
else:
	for i in range(m):
		(b, c) = [int(i) for i in input().split()]
		if d <= b:
			print(b)
			flag = 1
			break
		elif d <= c:
			print(d)
			flag = 1
			break
		else:
			pass
	if flag == 0:
		print(-1)


n = int(input())
arr = map(int, input().split())
sm = sum(arr)
f = -1
h = 0
k = int(input())
for i in range(k):
	(a, b) = map(int, input().split())
	if a <= sm <= b:
		f = sm
		h = 1
	elif a > sm and (not h):
		f = a
		h = 1
print(f)


n = int(input())
a = list(map(int, input().split()))
ans = -1
s = sum(a)
m = int(input())
for _ in range(m):
	(l, r) = map(int, input().split())
	if ans == -1:
		if l <= s <= r:
			ans = s
		elif s < l:
			ans = l
print(ans)


n = int(input())
arr = list(map(int, input().split()))
m = int(input())
timeStamps = []
for _ in range(m):
	(s, e) = map(int, input().split())
	timeStamps.append((s, e))
tot = sum(arr)
found = False
for t in timeStamps:
	(s, e) = (t[0], t[1])
	if tot < s:
		print(s)
		found = True
		break
	if tot >= s and tot <= e:
		print(tot)
		found = True
		break
if not found:
	print(-1)


n = int(input())
a = [int(x) for x in input().split()]
m = int(input())
t = []
for i in range(m):
	(x, y) = input().split()
	t.append([int(x), int(y)])
s = []
sum = 0
for i in a:
	sum += i
	s += [sum]
index = 0
tindex = 0
while index < n and tindex < m:
	if s[index] > t[tindex][1]:
		tindex += 1
		continue
	if s[index] >= t[tindex][0] and s[index] <= t[tindex][1]:
		if s[index] == t[tindex][1]:
			tindex += 1
		index += 1
	elif s[index] < t[tindex][0]:
		s[index] = t[tindex][0]
		index += 1
	else:
		tindex += 1
if index == n:
	print(s[index - 1])
else:
	print(-1)


n = int(input())
p = list(map(int, input().split()))
total = sum(p)
m = int(input())
t = -1
for _ in range(m):
	(s, e) = map(int, input().split())
	if s <= total and e >= total:
		t = total
		break
	elif min(s, e) >= total:
		t = s
		break
print(t)


n = int(input())
x = sum(list(map(int, input().split())))
n = int(input())
c = 0
for i in range(n):
	(a, b) = map(int, input().split())
	if c == 0:
		if x >= a and x <= b:
			r = x
			c = 1
		elif x <= a and x <= b:
			r = a
			c = 1
if c:
	print(r)
else:
	print('-1')


n = int(input())
list1 = list(map(int, input().strip().split(' ')))
m = int(input())
s = sum(list1)
list2 = []
for i in range(m):
	(a, b) = input().strip().split(' ')
	(a, b) = (int(a), int(b))
	list2.append((a, b))
if m == 0:
	print(-1)
	exit()
if s > list2[-1][1]:
	print(-1)
	exit()
else:
	for j in range(m):
		if list2[j][1] >= s:
			print(max(list2[j][0], s))
			exit()


n = input()
l = [int(x) for x in input().split()]
s = sum(l)
m = int(input())
ans = -1
for _ in range(m):
	(a, b) = input().split()
	(a, b) = (int(a), int(b))
	if s <= b and ans == -1:
		ans = max(a, s)
print(ans)


_ = int(input())
total_time = sum(map(int, input().rstrip().split()))
m = int(input())
for i in range(0, m):
	[start_time, end_time] = map(int, input().rstrip().split())
	if start_time >= total_time:
		print(start_time)
		exit()
	elif total_time <= end_time:
		print(total_time)
		exit()
print(-1)


n = int(input())
solve_time = sum([int(i) for i in input().split()])
result = float('inf')
flag = False
for _ in range(int(input())):
	(start, end) = [int(i) for i in input().split()]
	if result >= start >= solve_time:
		result = start
		flag = True
	elif end >= solve_time >= start:
		result = solve_time
		flag = True
if flag:
	print(result)
else:
	print(-1)


from collections import defaultdict
import sys, os, math

def __starting_point():
	n = int(input())
	arr1 = list(map(int, input().split()))
	m = int(input())
	sm = sum(arr1)
	ans = -1
	for i in range(m):
		(a, b) = list(map(int, input().split()))
		if a <= sm and sm <= b:
			ans = sm
		if ans == -1 and a > sm:
			ans = a
	print(ans)
__starting_point()


n = int(input())
tot = sum(map(int, input().split()))
m = int(input())
P = []
for i in range(m):
	(l, r) = map(int, input().split())
	P.append((l, r))
for (l, r) in P:
	if l <= tot and tot <= r:
		print(tot)
		exit(0)
	elif tot <= l:
		print(l)
		exit(0)
print(-1)


from sys import maxsize, stdout, stdin, stderr
import re
tup = lambda : map(int, stdin.readline().split())
I = lambda : int(stdin.readline())
lint = lambda : [int(x) for x in stdin.readline().split()]
S = lambda : stdin.readline().replace('\n', '').strip()

def grid(r, c):
	return [lint() for i in range(r)]

def debug(*args, c=6):
	print('\x1b[3{}m'.format(c), *args, '\x1b[0m', file=stderr)
I()
s = sum(lint())
for _ in range(I()):
	(l, r) = tup()
	if l <= s <= r:
		print(s)
		exit()
	elif s < l:
		print(l)
		exit()
print('-1')


f = lambda : map(int, input().split())
f()
(s, d) = (sum(f()), -1)
for i in range(int(input())):
	(l, r) = f()
	if s <= r:
		d = max(s, l)
		break
print(d)


n = int(input())
l = list(map(int, input().split()))
s = sum(l)
k = int(input())
for i in range(k):
	(l, r) = map(int, input().split())
	if l <= s <= r:
		print(s)
		break
	elif l > s:
		print(l)
		break
else:
	print(-1)


arri = lambda : [int(s) for s in input().split()]
n = int(input())
a = arri()
m = int(input())
t = []
for i in range(m):
	t.append(arri())
s = sum(a)
tx = -1
for i in t:
	if s >= i[0] and s <= i[1]:
		tx = s
		break
	elif s <= i[0]:
		tx = i[0]
		break
print(tx)


import sys
n = int(input())
a = [int(x) for x in input().split()]
m = int(input())
su = sum(a)
for _ in range(m):
	(x, y) = map(int, input().split())
	if y >= su:
		print(max(su, x))
		sys.exit()
print(-1)


from math import inf, sqrt, floor, ceil
from collections import Counter, defaultdict, deque
from heapq import heappush as hpush, heappop as hpop, heapify as h
from operator import itemgetter
from itertools import product
from bisect import bisect_left, bisect_right
n = int(input())
a = list(map(int, input().split()))
p = []
ans = 0
for _ in range(int(input())):
	(s1, s2) = map(int, input().split())
	p.append((s1, s2))
time = sum(a)
for (a, b) in p:
	if time <= a:
		time += a - time
		ans = 1
		break
	elif time >= a and time <= b:
		ans = 1
		break
if ans:
	print(time)
else:
	print(-1)


n = int(int(input()))
a = list(map(int, input().split()))
a = sum(a)
m = int(input())
for i in range(m):
	(x, y) = input().split()
	(x, y) = (int(x), int(y))
	if y >= a:
		print(max(a, x))
		break
else:
	print(-1)


i = int(input())
x = [int(i) for i in input().split()]
total_time = 0
for p in x:
	total_time += p
z = int(input())
flag = 0
for mrinal in range(z):
	(a, b) = map(int, input().split())
	if a <= total_time and b >= total_time:
		flag = total_time
		break
	elif total_time < a:
		flag = a
		break
if flag != 0:
	print(flag)
else:
	print(-1)


n = int(input())
a = list(map(int, input().split()))
m = int(input())
s = []
f = []
for mm in range(m):
	(ss, ff) = list(map(int, input().split()))
	s.append(ss)
	f.append(ff)
ft = 0
found = False
for i in a:
	ft += i
for i in range(len(s)):
	if s[i] <= ft and f[i] >= ft:
		print(ft)
		found = True
		break
	elif s[i] > ft:
		print(s[i])
		found = True
		break
if not found:
	print(-1)


import sys
n = int(sys.stdin.readline())
arr = list(map(int, sys.stdin.readline().split()))
arr.sort()
m = int(sys.stdin.readline())
time = []
for _ in range(m):
	(a, b) = map(int, sys.stdin.readline().split())
	time.append((a, b))
pf = [0] * n
pf[0] = arr[0]
for i in range(1, n):
	pf[i] = pf[i - 1] + arr[i]
for i in time:
	(start, end) = i
	if start <= pf[n - 1] <= end:
		print(pf[n - 1])
		sys.exit(0)
	elif start > pf[n - 1]:
		print(start)
		sys.exit(0)
print(-1)


n = int(input())
a = list(map(int, input().split()))
m = int(input())
j = []
for i in range(m):
	j.extend(list(map(int, input().split())))
tt = sum(a)
if m == 0 or tt > j[-1]:
	print(-1)
elif tt <= j[0]:
	print(j[0])
else:
	for i in range(0, len(j) - 1):
		if tt >= j[i] and tt <= j[i + 1]:
			if i % 2 == 0:
				print(tt)
			else:
				print(j[i + 1])
			exit()


n = int(input())
a = list(map(int, input().split()))
s = sum(a)
m = int(input())
ss = 10 ** 10
for i in range(m):
	(l, r) = map(int, input().split())
	if r < s:
		continue
	ss = min(ss, max(s, l))
if ss == 10 ** 10:
	print(-1)
else:
	print(ss)


n = int(input())
a = list(map(int, input().split()))
total = sum(a)
m = int(input())
earliest = 10 ** 6
for _ in range(m):
	(l, r) = map(int, input().split())
	if l <= total <= r:
		earliest = total
	elif total < l:
		earliest = min(earliest, l)
if earliest == 10 ** 6:
	print(-1)
else:
	print(earliest)


n = int(input())
a = list(map(int, input().split()))
s = sum(a)
m = int(input())
for _ in range(m):
	(a, b) = map(int, input().split())
	if b >= s:
		print(max(a, s))
		break
else:
	print(-1)


import itertools
import bisect
n = int(input())
s = sum(map(int, input().split()))
m = int(input())
b = list(itertools.chain(*(map(int, input().split()) for _ in range(m))))
i = bisect.bisect_left(b, s)
if i == len(b):
	s = -1
elif i % 2 == 0:
	s = b[i]
print(s)


n = int(input())
a = sum(map(int, input().split()))
m = int(input())
answer = -1
for i in range(m):
	(start, end) = map(int, input().split())
	if a < start:
		answer = start
		break
	if start <= a <= end:
		answer = a
		break
print(answer)


import sys
n = int(input())
a = sys.stdin.readline().split(' ')
for i in range(len(a)):
	a[i] = int(a[i])
m = int(input())
v = []
for i in range(m):
	line = sys.stdin.readline().split(' ')
	v.append((int(line[0]), int(line[1])))
v.sort(key=lambda tup: tup[0])
time = sum(a)
flag = False
totalTime = 0
for i in range(len(v)):
	if v[i][0] >= time:
		totalTime = v[i][0]
		break
	if v[i][1] >= time:
		totalTime = time
		break
if totalTime > 0:
	print(totalTime)
else:
	print(-1)


import sys
_ = input()
t = sum(map(int, input().split()))
for _ in range(int(input())):
	(l, r) = map(int, input().split())
	if t <= r:
		print(max(l, t))
		sys.exit()
print(-1)


n = int(input())
A = [int(i) for i in input().split()]
m = int(input())
period = []
for _ in range(m):
	period.append([int(i) for i in input().split()])
total = sum(A)
time = -1
for p in period:
	if total <= p[0]:
		time = p[0]
		break
	if total >= p[0] and total <= p[1]:
		time = total
		break
print(time)


n = int(input())
a = list(map(int, input().split()))
s = sum(a)
m = int(input())
for i in range(m):
	(a, b) = map(int, input().split())
	if a <= s <= b:
		print(s)
		exit()
	elif s <= a:
		print(a)
		exit()
print(-1)


n = int(input())
li = list(map(int, input().split()))
m = int(input())
a = []
for i in range(m):
	a.append(list(map(int, input().split())))
if m <= 0:
	print('-1')
elif a[m - 1][1] < sum(li):
	print('-1')
else:
	k = []
	for i in range(m):
		for j in range(a[i][0], a[i][1] + 1, 1):
			k.append(j)
	for c in k:
		if sum(li) - c <= 0:
			print(c)
			break


n = int(input())
a = sum(list(map(int, input().split())))
k = int(input())
l = 0
while k:
	k -= 1
	(s, l) = map(int, input().split())
	if l >= a:
		if s <= a:
			print(a)
		else:
			print(s)
		break
if l < a:
	print(-1)


I = lambda : map(int, input().split())
I()
(n, a) = (sum(I()), -1)
for _ in range(*I()):
	(l, r) = I()
	if n <= r:
		a = max(n, l)
		break
print(a)


n = int(input())
arr = [int(x) for x in input().split()]
s = sum(arr)
temp = []
for _ in range(int(input())):
	(l, r) = [int(x) for x in input().split()]
	temp.append([l, r])
ans = -1
for i in temp:
	if s >= i[0] and s <= i[1]:
		ans = s
		break
	elif i[0] > s:
		ans = i[0]
		break
print(ans)


n = int(input())
a = list(map(int, input().strip().split()))
s = sum(a)
t = int(input())
stor = -1
for i in range(t):
	(l, r) = list(map(int, input().strip().split()))
	if l <= s and s <= r and (stor == -1):
		stor = s
	if l > s and stor == -1:
		stor = l
print(stor)


n = int(input())
time = sum(list(map(int, input().split())))
m = int(input())
fin_time = -1
for i in range(m):
	(lj, rj) = map(int, input().split())
	if time <= rj:
		if time < lj:
			fin_time = lj
			break
		else:
			fin_time = time
			break
print(fin_time)


n = int(input())
a = list(map(int, input().split()))
sum_a = sum(a)
m = int(input())
for _ in range(m):
	(l, r) = map(int, input().split())
	if r < sum_a:
		continue
	print(max(sum_a, l))
	exit()
print(-1)


n = int(input())
pb = [int(i) for i in input().split(' ')]
tot = 0
for i in pb:
	tot += i
res = -1
q = int(input())
for i in range(q):
	pairs = [int(ii) for ii in input().split(' ')]
	if pairs[1] >= tot:
		res = max(pairs[0], tot)
		break
print(res)


import sys
import math
import bisect

def main():
	n = int(input())
	A = list(map(int, input().split()))
	val = sum(A)
	m = int(input())
	ans = -1
	for i in range(m):
		(l, r) = map(int, input().split())
		if val >= l and val <= r:
			ans = val
			break
		elif l > val:
			ans = l
			break
	print(ans)
main()


import math
n = int(input())
list = [int(i) for i in input().split()]
some = sum(list)
m = int(input())
for i in range(m):
	(a, b) = map(int, input().split())
	if some <= b:
		if some < a:
			print(a)
		else:
			print(some)
		exit()
print('-1')


I = lambda : map(int, input().split())
I()
a = sum(I())
m = int(*I())
for i in range(m):
	(l, r) = I()
	if a <= l <= r:
		print(l)
		exit(0)
	elif l <= a <= r:
		print(a)
		exit(0)
print(-1)


n = int(input())
a = [int(x) for x in input().split(' ')]
m = int(input())
res = sum(a)
isAns = False
for i in range(m):
	(l, r) = map(int, input().split(' '))
	if isAns:
		continue
	if l <= res <= r:
		isAns = True
	elif res < l:
		isAns = True
		res = l
if isAns:
	print(res)
else:
	print(-1)


def main():
	n = int(input())
	a = list(map(int, input().split()))
	m = int(input())
	x = []
	for _ in range(m):
		(l, r) = map(int, input().split())
		x.append((l, r))
	sa = sum(a)
	for i in range(m):
		if x[i][0] <= sa <= x[i][1]:
			return sa
		if sa <= x[i][0]:
			return x[i][0]
	return -1
print(main())


a = 0
c = input()
for x in list(map(int, input().split())):
	a += x
b = []
for x in range(int(input())):
	(i, j) = map(int, input().split())
	for y in range(i, j + 1):
		b.append(y)
for x in b:
	if a == x or a < x:
		print(x)
		break
else:
	print(-1)


R = lambda : list(map(int, input().strip().split()))
n = int(input())
a = R()
tot = sum(a)
m = int(input())
res = -1
for i in range(m):
	[l, r] = R()
	if r >= tot:
		res = max(l, tot)
		break
print(res)


input()
a = sum(map(int, input().split()))
n = int(input())
m = -1
for _ in range(n):
	i = list(map(int, input().split()))
	if m == -1 and a <= i[1]:
		m = max(i[0], a)
print(m)


input()
s = sum(map(int, input().split()))
m = int(input())
for _ in range(m):
	(l, r) = map(int, input().split())
	if r >= s:
		print(max(l, s))
		break
else:
	print(-1)


n = int(input())
a = [int(i) for i in input().split()]
m = int(input())
l = []
for i in range(m):
	k = [int(i) for i in input().split()]
	l.append((k[0], k[1]))
s = sum(a)
for i in l:
	if s <= i[1]:
		if s <= i[0]:
			print(i[0])
			break
		else:
			print(s)
			break
else:
	print(-1)


n = int(input().strip())
s = sum(map(int, input().strip().split()))
m = int(input().strip())
rls = [tuple(map(int, input().strip().split())) for _ in range(m)]
if m == 0 or s > rls[-1][1]:
	print(-1)
else:
	i = 0
	while rls[i][1] < s:
		i += 1
	print(max(rls[i][0], s))


input()
s = sum(map(int, input().split()))
m = int(input())
x = y = 0
for i in range(m):
	(x, y) = map(int, input().split())
	s = max(s, x)
	if s >= x and s <= y:
		print(s)
		break
if s > y or m == 0:
	print(-1)


__author__ = 'artyom'

def main():
	n = read()
	s = sum(read(3))
	for _ in range(read()):
		(l, r) = read(3)
		if l >= s:
			return l
		if r >= s:
			return s
	return -1

def read(mode=1, size=None):
	if mode == 0:
		return input().strip()
	if mode == 1:
		return int(input().strip())
	if mode == 2:
		return input().strip().split()
	if mode == 3:
		return list(map(int, input().strip().split()))
	a = []
	for _ in range(size):
		a.append(read(3))
	return a

def write(s='\n'):
	if s is None:
		s = ''
	if isinstance(s, tuple) or isinstance(s, list):
		s = ' '.join(map(str, s))
	s = str(s)
	print(s)
write(main())


R = lambda : list(map(int, input().split()))
input()
a = sum(R())
for i in range(int(input())):
	(c, d) = R()
	if c <= a <= d:
		print(a)
		return
	elif a < c:
		print(c)
		return
print(-1)


n = int(input())
a = list(map(int, input().split()))
m = int(input())
summ = sum(a)
res = -1
for i in range(m):
	lr = list(map(int, input().split()))
	if lr[0] <= summ and lr[1] >= summ:
		res = summ
		break
	if lr[0] >= summ:
		res = lr[0]
		break
print(res)


def solve(n, a, m, dur):
	tot = sum(a)
	for (l, r) in sorted(dur):
		if l <= tot and tot <= r:
			return tot
		elif tot < l:
			return l
	return -1

def main():
	n = int(input())
	a = [int(x) for x in input().split(' ')]
	m = int(input())
	dur = []
	for i in range(m):
		dur.append(tuple(map(int, input().split(' '))))
	print(solve(n, a, m, dur))
main()


n = int(input())
a = list(map(int, input().split()))
c = sum(a)
f = 0
b = int(input())
for i in range(b):
	(x, y) = list(map(int, input().split()))
	if c >= x and c <= y and (f == 0):
		print(c)
		f = 1
	elif c <= x and c <= y and (f == 0):
		print(x)
		f = 1
if f == 0:
	print(-1)


n = int(input())
st = sum(map(int, input().split()))
m = int(input())
for _ in range(m):
	(a, b) = list(map(int, input().split()))
	if st < a:
		print(a)
		break
	elif st <= b:
		print(st)
		break
else:
	print('-1')


n = int(input())
time = sum(map(int, input().split()))
m = int(input())
result = -1
for _ in range(m):
	(l, r) = map(int, input().split())
	if time <= r:
		result = max([time, l])
		break
print(result)


n = eval(input())
nArray = input().split()
totalTime = 0
for i in range(n):
	totalTime += eval(nArray[i])
m = eval(input())
mArray = []
for i in range(m):
	mArray.append(input().split())
possible = False
for i in range(m):
	if totalTime >= eval(mArray[i][0]) and totalTime <= eval(mArray[i][1]):
		possible = True
		print(totalTime)
		break
	elif i < m and totalTime <= eval(mArray[i][0]):
		possible = True
		print(mArray[i][0])
		break
if possible == False:
	print(-1)


I = lambda : map(int, input().split())
I()
t = sum(I())
for _ in range(int(input())):
	(L, R) = I()
	if t <= R:
		print(max(L, t))
		break
else:
	print(-1)


n = int(input())
t = list(map(int, input().split(' ')))
m = int(input())
work = []
for i in range(m):
	work.append(list(map(int, input().split(' '))))
S = sum(t)
if m == 0:
	print(-1)
elif S > work[-1][-1]:
	print(-1)
else:
	k = 0
	for w in work:
		if S >= w[0]:
			if S <= w[1]:
				k = 1
		else:
			break
	if k == 1:
		print(S)
	else:
		print(w[0])


import sys, math, itertools
from collections import Counter, deque, defaultdict
from bisect import bisect_left, bisect_right
from heapq import heappop, heappush, heapify
mod = 10 ** 9 + 7
INF = float('inf')

def inp():
	return int(sys.stdin.readline())

def inpl():
	return list(map(int, sys.stdin.readline().split()))

def inpl_1():
	return list(map(lambda x: int(x) - 1, sys.stdin.readline().split()))

def inps():
	return sys.stdin.readline()

def inpsl(x):
	tmp = inps()
	return list(tmp[:x])

def err(x):
	print(x)
	exit()
n = inp()
a = inpl()
m = inp()
seg = [inpl() for _ in range(m)]
seg.sort()
res = 0
a = [sum(a)]
for x in a:
	for (l, r) in seg:
		if l <= x <= r:
			res += x
			break
		elif x < l:
			res += l
			break
	else:
		print(-1)
		break
else:
	print(res)


n = int(input())
a = list(map(int, input().split()))
m = int(input())
p = []
if m == 0:
	print(-1)
else:
	for i in range(m):
		(a1, b1) = map(int, input().split())
		p.append(a1)
		p.append(b1)
	m1 = max(p)
	s = sum(a)
	if s > m1:
		print(-1)
	else:
		for i in range(len(p)):
			if p[i] > s:
				if i & 1:
					print(s)
					break
				else:
					print(p[i])
					break
			elif p[i] == s:
				print(s)
				break
			else:
				pass


n = int(input())
probs = [int(p) for p in input().split()]
time = sum(probs)
windows = []
times = {}
last = 0
m = int(input())
if m == 0:
	print(-1)
	exit()
for bruh in range(m):
	interval = [int(b) for b in input().split()]
	for na in range(last, interval[0]):
		times[na] = False
	for ye in range(interval[0], interval[1] + 1):
		times[ye] = True
	last = interval[1] + 1
if time > max(times):
	print(-1)
for x in range(max(times) - time + 1):
	if times[time + x] == True:
		print(time + x)
		exit()


n = int(input())
a = list(map(int, input().split()))
m = int(input())
p = []
for i in range(m):
	(l, r) = map(int, input().split())
	p.append((l, r))
rs = sum(a)
for i in range(len(p)):
	if p[i][0] <= rs <= p[i][1]:
		print(rs)
		exit(0)
	if p[i][0] > rs:
		print(p[i][0])
		exit(0)
print(-1)


import sys
import string
from collections import Counter, defaultdict
from math import fsum, sqrt, gcd, ceil, factorial
from itertools import combinations, permutations
flush = lambda : sys.stdout.flush
comb = lambda x, y: factorial(x) // factorial(y) // factorial(x - y)
ip = lambda : input()
ii = lambda : int(input())
r = lambda : map(int, input().split())
rr = lambda : list(r())
n = ii()
arr = rr()
k = ii()
brr = [rr() for _ in range(k)]
x = sum(arr)
for i in range(k):
	if brr[i][0] <= x <= brr[i][1]:
		print(x)
		exit()
	if brr[i][0] > x:
		print(brr[i][0])
		exit()
print(-1)


n = int(input())
t = sum(map(int, input().split()))
m = int(input())
for _ in range(m):
	(l, r) = list(map(int, input().split()))
	if t <= r:
		print(max(t, l))
		quit()
print(-1)


from bisect import bisect
n = int(input())
a = list(map(int, input().split()))
(m, s, t) = (int(input()), sum(a), [])
if m == 0:
	print(-1)
else:
	for _ in range(m):
		(l, r) = map(int, input().split())
		t += range(l, r + 1)
	if s in t:
		print(s)
	else:
		try:
			print(t[bisect(t, s)])
		except IndexError:
			print(-1)


def rint():
	return int(input())

def rints():
	return list(map(int, input().split()))
n = rint()
s = sum(rints())
m = rint()
for _ in range(m):
	(l, r) = rints()
	if s <= r:
		print(max(l, s))
		exit(0)
print(-1)


n = int(input())
line = input()
su = line.split()
s = 0
ar = list()
for i in su:
	s = s + int(i)
t = int(input())
for i in range(t):
	line = input()
	pui = line.split()
	l = int(pui[0])
	r = int(pui[1])
	ar.append((l, r))
tot = None
for (l, r) in ar:
	if s >= l and s <= r:
		tot = s
		break
	elif s <= l:
		tot = l
		break
if tot is None:
	print('-1')
else:
	print(tot)


n = int(input())
l = [int(x) for x in input().split()]
t = 0
for i in range(n):
	t += int(l[i])
m = int(input())
mi = 1000000
for i in range(m):
	(a, b) = map(int, input().split())
	if (a == t or b == t) or a < t < b:
		mi = t
	elif a > t & b > t:
		mi = min(mi, a, b)
	elif a > t:
		mi = min(mi, a)
	elif b > t:
		mi = min(mi, b)
print(mi if mi < 1000000 else -1)


from sys import stdin, stderr

def readInts():
	return map(int, stdin.readline().strip().split())

def print_err(*args, **kwargs):
	print(*args, file=stderr, **kwargs)

def solve(vs):
	return None

def run():
	(n,) = readInts()
	a = readInts()
	(m,) = readInts()
	time_all = sum(a)
	printed = False
	for _ in range(m):
		(lj, rj) = readInts()
		if printed:
			continue
		if time_all <= lj:
			print(lj)
			printed = True
		elif time_all <= rj:
			print(time_all)
			printed = True
	if not printed:
		print(-1)
run()


def competition(a, lst):
	s = sum(a)
	for elem in lst:
		if s <= elem[1]:
			if s >= elem[0]:
				return s
			else:
				return elem[0]
	return -1
n = int(input())
b = [int(i) for i in input().split()]
c = list()
m = int(input())
for z in range(m):
	(l, r) = [int(x) for x in input().split()]
	c.append([l, r])
print(competition(b, c))


(n, m) = (0, 0)
tasks = []
periods = []

def getInput():
	global n, tasks, periods, m
	n = int(input())
	tasks = [int(x) for x in input().split()]
	m = int(input())
	for i in range(m):
		periods.append([int(x) for x in input().split()])

def getListSum(l):
	sum = 0
	for i in l:
		sum = sum + i
	return sum

def defineInterval(sum, periods):
	for interval in periods:
		(l, r) = (interval[0], interval[1])
		if sum <= l:
			return l
		if l <= sum <= r:
			return sum
	return -1

def main():
	global n, tasks, periods
	getInput()
	sum = getListSum(tasks)
	print(defineInterval(sum, periods))
main()


n = int(input())
a = tuple(map(int, input().split()))
A = sum(a)
found = False
m = int(input())
for i in range(m):
	(left, right) = map(int, input().split())
	if left <= A <= right or A < left:
		print(max(left, A))
		found = True
		break
if not found:
	print(-1)


def find(x, y, n):
	for i in range(x, y + 1):
		if i >= n:
			return i
	return 0
n = int(input())
a = list(map(int, input().split()))
m = int(input())
f = True
for i in range(m):
	(x, y) = map(int, input().split())
	if find(x, y, sum(a)) != 0 and f:
		print(find(x, y, sum(a)))
		f = False
if f:
	print(-1)


n = int(input())
l = list(map(int, input().strip().split(' ')))
ans = sum(l)
t = int(input())
p = -1
flag = True
for _ in range(t):
	(l, h) = list(map(int, input().strip().split(' ')))
	if l <= ans and ans <= h and flag:
		p = ans
		flag = False
	if ans < l and flag:
		p = l
		flag = False
print(p)


n = int(input())
s = sum(map(int, input().split()))
m = int(input())
st = False
for i in range(m):
	(l, r) = map(int, input().split())
	if s <= r:
		print(max(l, s))
		st = True
		break
if not st:
	print(-1)


class Object(object):
	pass

def read_input():
	result = Object()
	n = int(input())
	result.times = list(map(int, input().split()))
	m = int(input())
	periods = list()
	for i in range(0, m):
		periods.append(list(map(int, input().split())))
	result.periods = periods
	return result

def find_submition_time(times, periods):
	total_time = sum(times)
	result = -1
	for period in periods:
		if total_time in range(period[0], period[1] + 1):
			return total_time
		if period[0] >= total_time:
			if result == -1:
				result = period[0]
			else:
				result = min(result, period[0])
	return result
result = read_input()
print(find_submition_time(result.times, result.periods))


import sys
input = sys.stdin.readline

def inp():
	return int(input())

def inara():
	return list(map(int, input().split()))

def insr():
	s = input()
	return list(s[:len(s) - 1])

def invr():
	return map(int, input().split())
n = inp()
a = inara()
tot = sum(a)
m = inp()
for i in range(m):
	(l, r) = invr()
	if l <= tot and r >= tot:
		print(tot)
		exit(0)
	elif l > tot:
		print(l)
		exit(0)
print(-1)


n = int(input())
l = list(map(int, input().split()))
s = sum(l)
q = int(input())
l = []
for i in range(q):
	l.append(list(map(int, input().split())))
ans = int(1000000.0)
for i in l:
	if s <= i[0]:
		ans = min(ans, i[0])
	elif i[0] <= s <= i[1]:
		ans = min(ans, s)
if ans > 100000:
	ans = -1
print(ans)


n = int(input())
ps = list(map(int, input().split()))
m = int(input())
durs = [list(map(int, input().split())) for i in range(m)]
s = sum(ps)

def f():
	for i in range(m):
		if durs[i][0] > s:
			return durs[i][0]
		if durs[i][1] >= s:
			return s
	return -1
print(f())


def solve():
	strInput = input()
	n = int(strInput)
	strInput = input()
	times = strInput.split()
	totalSolvingTime = 0
	for s in times:
		totalSolvingTime += int(s)
	strInput = input()
	m = int(strInput)
	for i in range(m):
		fullRange = input()
		boundaries = fullRange.split()
		if totalSolvingTime > int(boundaries[1]):
			continue
		else:
			left = int(boundaries[0])
			if left >= totalSolvingTime:
				return left
			else:
				return totalSolvingTime
	return -1
print(solve())


n = int(input())
a = list(map(int, input().split()))
tot = 0
for e in a:
	tot += e
m = int(input())
for i in range(m):
	[l, r] = list(map(int, input().split()))
	if l <= tot and tot <= r:
		print(tot)
		exit(0)
	elif tot < l:
		print(l)
		exit(0)
print('-1')


n = int(input())
a = list(map(int, input().split()))
m = int(input())
b = [0]
prev_r = 0
for i in range(m):
	(l, r) = map(int, input().split())
	b = b + (l - prev_r - 1) * [0]
	b = b + (r - l + 1) * [1]
	prev_r = r
s = sum(a)
for i in range(s, len(b)):
	if b[i] == 1:
		print(i)
		exit()
print(-1)


from sys import stdin
import re

def readInt(count=1):
	m = re.match('\\s*' + '([+-]?\\d+)\\s*' * count, stdin.readline())
	if m is not None:
		ret = []
		for i in range(1, m.lastindex + 1):
			ret.append(int(m.group(i)))
		return ret
	return None
(taskCount,) = readInt()
thinkingTime = readInt(taskCount)
(onlineCount,) = readInt()
online = []
for i in range(onlineCount):
	online.append(readInt(2))
totalThinking = sum(thinkingTime)
best = -1
for i in range(onlineCount):
	if online[i][0] >= totalThinking:
		if best == -1 or online[i][0] < best:
			best = online[i][0]
	elif online[i][1] >= totalThinking:
		best = totalThinking
		break
print(best)


numProb = int(input())
numLen = 0
inp = input().split(' ')
for i in range(len(inp)):
	numLen += int(inp[i])
numInt = int(input())
prt = False
for i in range(numInt):
	inp = input().split(' ')
	beg = int(inp[0])
	end = int(inp[1])
	if numLen >= beg and numLen <= end:
		print(numLen)
		prt = True
		break
	elif beg >= numLen:
		print(beg)
		prt = True
		break
if prt == False:
	print(-1)


import math
n = input()
a = input().split(' ')
m = int(input())
arr = []
for i in range(m):
	lr = input().split(' ')
	arr.append([int(num) for num in lr])
a = [int(num) for num in a]
s = sum(a)
for i in arr:
	if s <= i[1]:
		if s >= i[0]:
			print(s)
		else:
			print(i[0])
		quit()
print(-1)


def main():
	n = int(input())
	s = sum((int(c) for c in input().split()))
	m = int(input())
	for _ in range(m):
		(l, r) = map(int, input().split())
		if l <= s <= r:
			print(s)
			return
		elif s < l:
			print(l)
			return
	print(-1)
main()


n = int(input())
time = [int(x) for x in input().split()]
m = int(input())
periods = []
for i in range(m):
	(l, r) = (int(x) for x in input().split())
	periods.append((l, r))
summ = sum(time)
for (l, r) in periods:
	if l <= summ and r >= summ:
		print(summ)
		break
	elif l >= summ:
		print(l)
		break
else:
	print('-1')


n = int(input())
s = sum([int(i) for i in input().split()])
m = int(input())
ans = -1
while m > 0:
	(l, r) = map(int, input().split())
	if max(l, s) == min(s, r):
		ans = s
		break
	if max(l, s) == l:
		ans = l
		break
	m -= 1
print(ans)


import sys, math, queue, bisect
MOD = 10 ** 9 + 7
sys.setrecursionlimit(1000000)
n = int(input())
s = sum(map(int, input().split()))
ans = -1
for _ in range(int(input())):
	(l, r) = map(int, input().split())
	if s <= r:
		if ans == -1:
			ans = max(s, l)
		else:
			ans = min(ans, max(s, l))
print(ans)


n = int(input())
time = list(map(int, input().split()))
m = int(input())
work = []
for i in range(m):
	work.append(list(map(int, input().split())))
whole_time = sum(time)
work = list(filter(lambda x: x[1] >= whole_time, work))
if len(work) == 0:
	print(-1)
elif min(work)[0] > whole_time:
	print(min(work)[0])
else:
	print(whole_time)


input()
a = sum(list(map(int, input().split())))
b = -1
c = int(input())
for i in range(c):
	(d, f) = list(map(int, input().split()))
	if d <= a and a <= f:
		b = a
		break
	if a <= d:
		b = d
		break
print(b)


import sys
n = int(input())
arr = list(map(int, input().split()))
s = sum(arr)
m = int(input())
s2 = 0
l = r = 0
for i in range(m):
	(l, r) = map(int, input().split())
	if s2 == 0:
		if l <= s and s <= r:
			s2 = s
		elif s < l:
			s2 = l
if s <= r:
	print(s2)
else:
	print('-1')


n = int(input())
a = list(map(int, input().split()))
m = int(input())
per = []
tt = sum(a)
f = 0
for _ in range(m):
	(l, r) = map(int, input().split())
	if l <= tt and tt <= r:
		print(tt)
		f = 1
		break
	elif l > tt and f == 0:
		print(l)
		f = 1
		break
if f == 0:
	print(-1)


n = int(input())
a = list(map(int, input().split()))
m = int(input())
sub = [0] * 100000
for _ in range(m):
	(l, r) = map(int, input().split())
	l -= 1
	r -= 1
	for i in range(l, r + 1):
		sub[i] = 1
last = sum(a)
ans = -1
for i in range(last - 1, 100000):
	if sub[i]:
		ans = i + 1
		break
print(ans)


from sys import stdin as fin
n = int(fin.readline())
tasks = list(map(int, fin.readline().split()))
m = int(fin.readline())
segs = [tuple(map(int, fin.readline().split())) for i in range(m)]
t_end = sum(tasks)
segs.sort()
for (s, t) in segs:
	if s <= t_end <= t:
		print(t_end)
		break
	if t_end < s:
		print(s)
		break
else:
	print(-1)


import sys
from bisect import bisect_left as lb
from collections import deque
from queue import PriorityQueue as pq
from math import *
input_ = lambda : sys.stdin.readline().strip('\r\n')
ii = lambda : int(input_())
il = lambda : list(map(int, input_().split()))
ilf = lambda : list(map(float, input_().split()))
ip = lambda : input_()
fi = lambda : float(input_())
ap = lambda ab, bc, cd: ab[bc].append(cd)
li = lambda : list(input_())
pr = lambda x: print(x)
prinT = lambda x: print(x)
f = lambda : sys.stdout.flush()
inv = lambda x: pow(x, mod - 2, mod)
mod = 10 ** 9 + 7
n = ii()
a = il()
s = sum(a)
m = ii()
for i in range(m):
	(l, r) = il()
	if s <= l:
		print(l)
		exit(0)
	elif l <= s <= r:
		print(s)
		exit(0)
print(-1)


n = int(input())
l = list(map(int, input().split()))
s = sum(l)
m = int(input())
flag = 0
ans = -1
for i in range(m):
	(a, b) = map(int, input().split())
	if s <= a:
		flag = 1
		ans = a
		break
	elif s > a and s < b:
		flag = 1
		ans = s
		break
	elif s == b:
		flag = 1
		ans = s
		break
if flag == 1:
	print(ans)
else:
	print(ans)


n = int(input())
last = sum(map(int, input().split()))
m = int(input())
for i in range(m):
	(a, b) = map(int, input().split())
	if last <= b:
		print(max(last, a))
		exit(0)
print(-1)


n = int(input())
a = list(input().split())
sum = 0
for i in a:
	sum += int(i)
m = int(input())
a = True
for i in range(m):
	(l, r) = map(int, list(input().split()))
	if sum <= r:
		if l <= sum:
			print(sum)
			a = False
			break
		else:
			print(l)
			a = False
			break
if a:
	print(-1)


def solve():
	n = int(input())
	a = [int(s) for s in input().split(' ')]
	x = sum(a)
	m = int(input())
	u = [tuple([int(x) for x in input().split(' ')]) for i in range(m)]
	for (l, r) in u:
		if r >= x:
			return max(l, x)
	return -1
print(solve())


n = int(input())
arr = [int(x) for x in input().split()]
m = int(input())
l = [0] * m
r = [0] * m
if m == 0:
	print(-1)
	exit(0)
for i in range(m):
	(l[i], r[i]) = [int(x) for x in input().split()]
s = sum(arr)
for i in range(m):
	if l[i] <= s and r[i] >= s:
		print(s)
		exit(0)
for i in range(m):
	if s <= l[i]:
		print(l[i])
		exit(0)
print(-1)