Anton likes to play chess. Also he likes to do programming. No wonder that he decided to attend chess classes and programming classes.

Anton has n variants when he will attend chess classes, i-th variant is given by a period of time (l_{1, }i, r_{1, }i). Also he has m variants when he will attend programming classes, i-th variant is given by a period of time (l_{2, }i, r_{2, }i).

Anton needs to choose exactly one of n possible periods of time when he will attend chess classes and exactly one of m possible periods of time when he will attend programming classes. He wants to have a rest between classes, so from all the possible pairs of the periods he wants to choose the one where the distance between the periods is maximal.

The distance between periods (l_1, r_1) and (l_2, r_2) is the minimal possible distance between a point in the first period and a point in the second period, that is the minimal possible |i - j|, where l_1 ≤ i ≤ r_1 and l_2 ≤ j ≤ r_2. In particular, when the periods intersect, the distance between them is 0.

Anton wants to know how much time his rest between the classes will last in the best case. Help Anton and find this number!


-----Input-----

The first line of the input contains a single integer n (1 ≤ n ≤ 200 000) — the number of time periods when Anton can attend chess classes.

Each of the following n lines of the input contains two integers l_{1, }i and r_{1, }i (1 ≤ l_{1, }i ≤ r_{1, }i ≤ 10^9) — the i-th variant of a period of time when Anton can attend chess classes.

The following line of the input contains a single integer m (1 ≤ m ≤ 200 000) — the number of time periods when Anton can attend programming classes.

Each of the following m lines of the input contains two integers l_{2, }i and r_{2, }i (1 ≤ l_{2, }i ≤ r_{2, }i ≤ 10^9) — the i-th variant of a period of time when Anton can attend programming classes.


-----Output-----

Output one integer — the maximal possible distance between time periods.


-----Examples-----
Input
3
1 5
2 6
2 3
2
2 4
6 8

Output
3

Input
3
1 5
2 6
3 7
2
2 4
1 4

Output
0



-----Note-----

In the first sample Anton can attend chess classes in the period (2, 3) and attend programming classes in the period (6, 8). It's not hard to see that in this case the distance between the periods will be equal to 3.

In the second sample if he chooses any pair of periods, they will intersect. So the answer is 0.

Difficulty: interview

Starter code:

Solutions:
import math
import itertools


def ria():
    return [int(i) for i in input().split()]


mx = 0

sz = ria()[0]
mx1 = 0
mn1 = 2000000000

for i in range(sz):
    l, r = ria()
    mx1 = max(l, mx1)
    mn1 = min(r, mn1)

sz = ria()[0]
mx2 = 0
mn2 = 2000000000

for i in range(sz):
    l, r = ria()
    mx2 = max(l, mx2)
    mn2 = min(r, mn2)
#print(mx1,mn1,mx2,mn2)
mx = max(mx, mx1 - mn2)
mx = max(mx, mx2 - mn1)
print(mx)

from operator import itemgetter
n = int(input())
a = [list(map(int, input().split())) for i in range(n)]
m = int(input())
b = [list(map(int, input().split())) for i in range(m)]
am = min(a, key = itemgetter(1))[1]
ap = max(a, key = itemgetter(0))[0]
bm = min(b, key = itemgetter(1))[1]
bp = max(b, key = itemgetter(0))[0]

ab = max(ap - bm, 0)
ba = max(bp - am, 0)
print(max(ab, ba))


n = int(input())
mx = 0
mn = 10 ** 10
for i in range(n):
    l, r = list(map(int,input().split(' ')))
    mx = max(mx,l)
    mn = min(mn,r)

m = int(input())
ans = 0
for i in range(m):
    l, r = list(map(int,input().split(' ')))
    ans = max([ans,mx-r,l-mn])
print(ans)


n = int(input())
a = 10000000000
a1 = -10000000000
for i in range(n):
    x, y = map(int, input().split())
    a = min(a, y)
    a1 = max(a1, x)
m = int(input())
b = -10000000000
b1 = 10000000000
for i in range(m):
    x, y = map(int, input().split())
    b = max(b, x)
    b1 = min(b1, y)
print(max(0, b - a, a1 - b1))

n = int(input())
a = []
for i in range(n):
    a.append(list(map(int,input().split())))
m = int(input())
b = []
for i in range(m):
    b.append(list(map(int,input().split())))    
a.sort()
b1 = b[::]
b.sort()
b1.sort(key = lambda x: x[1])
ans = 0
for i in a:
    ans = max(ans,b[-1][0]-i[1])
    ans = max(ans,i[0]-b1[0][1])
print(ans)

l1 = [[int(x.strip().rstrip()) for x in input().split()] for _ in range(int(input()))]
l2 = [[int(x.strip().rstrip()) for x in input().split()] for _ in range(int(input()))]

r1 = max(0, max(x[0] for x in l2) - min([x[1] for x in l1]))
r2 = max(0, max(x[0] for x in l1) - min([x[1] for x in l2]))

print(max(r1, r2))


n = int(input())


chess_start = 0
chess_end = 100000000000
prog_start = 0
prog_end = 100000000000
for i in range(n):
	s, f = list(map(int, input().split()))
	if s > chess_start:
		chess_start = s
	if f < chess_end:
		chess_end = f
		
m = int(input())
for i in range(m):
	s, f = list(map(int, input().split()))
	if s > prog_start:
		prog_start = s
	if f < prog_end:
		prog_end = f

chessfirst = prog_start - chess_end

progfirst = chess_start - prog_end

print(max(0, chessfirst, progfirst))




n = int(input())
mnc,mxc,mnp,mxp=1e10,0,1e10,0
for i in range(n):
	a,b = list(map(int,input().split()))
	mxc = max(mxc,a)
	mnc = min(mnc,b)
	
n2 = int(input())
for i in range(n2):
	a,b = list(map(int,input().split()))
	mxp = max(mxp,a)
	mnp = min(mnp,b)
val = max(mxp-mnc,mxc-mnp)
if val<0:
	print(0)
else:
	print(val)


from sys import stdin, stdout

n = int(stdin.readline())
chessStart=[]
chessEnd=[]
for i in range(n):
    l, r = list(map(int, stdin.readline().rstrip().split()))
    chessStart.append(l)
    chessEnd.append(r)
    
m = int(stdin.readline())
programStart=[]
programEnd=[]
for i in range(m):
    l, r = list(map(int, stdin.readline().rstrip().split()))
    programStart.append(l)
    programEnd.append(r)

chessEarlyEnd=min(chessEnd)
chessLateStart=max(chessStart)
programEarlyEnd=min(programEnd)
programLateStart=max(programStart)

bestCase=max([chessLateStart-programEarlyEnd,programLateStart-chessEarlyEnd])
print(max([0,bestCase]))


n = int(input())
maxl1 = 0
minr1 = 100000000000000
for _ in range(0,n):
    a,b = list(map(int,input().split()))
    maxl1 = max(a,maxl1)
    minr1 = min(b,minr1)

n = int(input())
maxl2 = 0
minr2 = 100000000000000
for _ in range(0,n):
    a,b = list(map(int,input().split()))
    maxl2 = max(a,maxl2)
    minr2 = min(b,minr2)

#print(maxl1,minr1,maxl2,minr2)

print(max(max(0, maxl1-minr2), max(0, maxl2-minr1)))


n=int(input())
chess=list()
for i in range(n):
    a,b=list(map(int,input().split()))
    chess.append([a,b])
m=int(input())
pro=list()
for i in range(m):
    a,b=list(map(int,input().split()))
    pro.append([a,b])
l1,e1,l2,e2=0,1000000000,0,1000000000
for i in range(n):
    l1=max(chess[i][0],l1)
    e1=min(chess[i][1],e1)
for i in range(m):
    l2=max(pro[i][0],l2)
    e2=min(pro[i][1],e2)
print(max([0,l2-e1,l1-e2]))

minn = 999999999999
maxn = 0
minm = 999999999999
maxm = 0

for n in range(int(input())):
    l, r = map(int, input().split())
    minn = min(minn, r)
    maxn = max(maxn, l)

for m in range(int(input())):
    l, r = map(int, input().split())
    minm = min(minm, r)
    maxm = max(maxm, l)

print(max([maxm - minn, maxn - minm, 0]))

n = int(input())
mr = 10**10
ml2 = 0
for _ in range(n):
    a, b = list(map(int, input().split()))
    mr = min(mr, b)
    ml2 = max(ml2, a)

ml = 0
mr2 = 10**10
n = int(input())
for _ in range(n):
    a, b = list(map(int, input().split()))
    ml = max(ml, a)
    mr2 = min(mr2, b)
print(max(0, ml-mr, ml2-mr2))


n = int(str(input()).strip())
min_i_r = 10**9+1
max_i_l = 0
for _ in range(n):
    l, r = str(input()).strip().split(' ', 1)
    min_i_r = min(min_i_r, int(r))
    max_i_l = max(max_i_l, int(l))

m = int(str(input()).strip())
min_j_r = 10**9+1
max_j_l = 0
for _ in range(m):
    l, r = str(input()).strip().split(' ', 1)
    min_j_r = min(min_j_r, int(r))
    max_j_l = max(max_j_l, int(l))

ans = max(max_j_l - min_i_r, max_i_l - min_j_r)
print(ans if ans > 0 else 0)


n1 = int(input())

start1, end1 = list(map(int, input().split()))

n1 -= 1

for _ in range(n1):
    s1, e1 = list(map(int, input().split()))
    if s1 > start1:
        start1 = s1
    if e1 < end1:
        end1 = e1

n2 = int(input())

start2, end2 = list(map(int, input().split()))

n2 -= 1

for _ in range(n2):
    s2, e2 = list(map(int, input().split()))
    if s2 > start2:
        start2 = s2
    if e2 < end2:
        end2 = e2

gap = max(0, start2-end1, start1-end2)
print(gap)


n = int(input())
lc = 0
rc = 10 ** 9 + 1
for i in range(n):
    l, r = map(int, input().split())
    lc = max(l, lc)
    rc = min(r, rc)
m = int(input())
lp = 0
rp = 10 ** 9 + 1
for i in range(m):
    l, r = map(int, input().split())
    lp = max(l, lp)
    rp = min(r, rp)
print(max(0, lp - rc, lc - rp))

n = int(input())
a = []
for _ in range(n):
	l, r = list(map(int, input().split()))
	a.append([l, r])

m = int(input())
b = []
for _ in range(m):
	l, r = list(map(int, input().split()))
	b.append([l, r])

b.sort()
res = 0
for i in a:
	res = max(b[m - 1][0] - i[1], res)

a.sort()
for i in b:
	res = max(a[n - 1][0] - i[1], res)
	
print(res)


#!/usr/bin/env python3
from sys import stdin,stdout


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

n = int(input())
minnr = 10**9+1
maxnl = -1
minmr = 10**9+1
maxml = -1
for i in range(n):
    l, r = ri()
    minnr = min(minnr, r)
    maxnl = max(maxnl, l)

m = int(input())
for i in range(m):
    l, r = ri()
    minmr = min(minmr, r)
    maxml = max(maxml, l)

ans = 0
if minnr <= maxml:
    ans = maxml - minnr
else:
    ans = 0


if minmr <= maxnl:
    ans = max(ans, maxnl-minmr)

print(ans)



n = int(input())
smin = 10**9
smax = 0
for i in range(n):
	(l, r) = (int(i) for i in input().split())
	if r<smin: smin = r
	if l>smax: smax = l


m = int(input())
pmin = 10**9
pmax = 0
for i in range(m):
	(l, r) = (int(i) for i in input().split())
	if r<pmin: pmin = r
	if l>pmax: pmax = l

	
print(max(smax-pmin,pmax-smin, 0))


n = int(input())

ms1 = 0
mt1 = 20000000000

ms2 = 0
mt2 = 20000000000

for _ in range(n):     
    s, t = [int(x) for x in input().split()]
    ms1 = max(ms1, s)
    mt1 = min(mt1, t)
      
m = int(input())

for _ in range(m): 
    s, t = [int(x) for x in input().split()]
    ms2 = max(ms2, s)
    mt2 = min(mt2, t)
    
dif = max(ms1 - mt2, ms2 - mt1)
print(max(0, dif))

import sys
n=int(input())
min=sys.maxsize
max1=0
for _ in range(n):
    a,b=map(int,input().split())
    if min>b:
        min=b
    if max1<a:
        max1=a
m=int(input())
max=0
min1=sys.maxsize
for _ in range(m):
    a,b=map(int,input().split())
    if max<a:
        max=a
    if min1>b:
        min1=b
if max<=min:
    ans=0
else:
    ans=max-min
if max1<=min1:
    ans1=0
else:
    ans1=max1-min1
if ans>ans1:
    print(ans)
else:
    print(ans1)

n=int(input())
ln=sorted(list(map(int,input().split())) for _ in range(n))
m=int(input())
lm=sorted(list(map(int,input().split())) for _ in range(m))
lln=sorted(x[::-1] for x in ln)
llm=sorted(x[::-1] for x in lm)
ans=ln[-1][0]-llm[0][0]
if lm[-1][0]-lln[0][0]>ans: ans=lm[-1][0]-lln[0][0]
print(max(0,ans))

"""Codeforces Round #404 (Div. 2)

B. Anton and Classes
"""


def dist_interval(a, b):
    p0, q0 = a
    p1, q1 = b
    if not (q0 <= p1 or q1 <= p0):
        return 0
    return min(abs(p1 - q0), abs(q1 - p0))


def main():
    n_chess_classes = int(input())
    chess_interval = [tuple(map(int, input().split()))
                      for _ in range(n_chess_classes)]

    m_prog_classes = int(input())
    prog_interval = [tuple(map(int, input().split()))
                     for _ in range(m_prog_classes)]

    ans = max(dist_interval(min(chess_interval, key=lambda x: x[1]),
                            max(prog_interval)),
              dist_interval(min(prog_interval, key=lambda x: x[1]),
                            max(chess_interval)))
    print(ans)


def __starting_point():
    main()

__starting_point()

inp=int(input())
minlchess=0
minrchess=10000000000
for i in range(inp):
    s=list(map(int,input().split()))
    leftchess=s[0]
    rightchess=s[1]
    if leftchess>minlchess:
        minlchess=leftchess
    if rightchess<minrchess:
        minrchess=rightchess
inp=int(input())
minlprog=0
minrprog=10000000000
for i in range(inp):
    s=list(map(int,input().split()))
    leftprog=s[0]
    rightprog=s[1]
    if leftprog>minlprog:
        minlprog=leftprog
    if rightprog<minrprog:
        minrprog=rightprog
dif1=minlprog-minrchess
dif2=minlchess-minrprog
if dif1>dif2:
    if dif1>0:
        print(dif1)
    else:
        print('0')
else:
    if dif2>0:
        print(dif2)
    else:
        print('0')

#!/bin/python3
arrn=[]
arrm=[]
maxstn=-1
minendn=10**10
maxstm=-1
minendm=10**10
n=int(input())
for i in range(n):
    arrn.append([int(x) for x in input().strip().split()])
    if arrn[i][0]>maxstn:
        maxstn=arrn[i][0]
    if arrn[i][1]<minendn:
        minendn=arrn[i][1]

m=int(input())
for i in range(m):
    arrm.append([int(x) for x in input().strip().split()])
    if arrm[i][0]>maxstm:
        maxstm=arrm[i][0]
    if arrm[i][1]<minendm:
        minendm=arrm[i][1]
ans=0
if maxstn-minendm>ans:
    ans=maxstn-minendm
if maxstm-minendn>ans:
    ans=maxstm-minendn
print(ans)