from collections import defaultdict class Segment_Tree: def __init__(self,N,f,e,lst=None,dynamic=False,bisect_search=True): self.f=f self.e=e self.N=N self.bisect_search=bisect_search if self.bisect_search: self.le=1 while self.le1: i>>= 1 self.segment_tree[i]=self.f(self.segment_tree[i<<1],self.segment_tree[i<<1|1]) def Build(self,lst): for i,x in enumerate(lst,self.le): self.segment_tree[i]=x for i in range(self.le-1,0,-1): self.segment_tree[i]=self.f(self.segment_tree[i<<1],self.segment_tree[i<<1|1]) def Fold(self,L=None,R=None): if L==None: L=self.le else: assert 0<=L<=self.N L+=self.le if R==None: R=self.le*2 else: assert 0<=R<=self.N R+=self.le vL=self.e vR=self.e while L>=1 R>>=1 return self.f(vL,vR) def Fold_Index(self,L=None,R=None): if L==None: L=self.le else: assert 0<=L<=self.N L+=self.le if R==None: R=self.le*2 else: assert 0<=R<=self.N R+=self.le if L==R: return None x=self.Fold(L-self.le,R-self.le) while L>=1 R>>=1 while i>=1 r>>=1 if f(self.f(vl,vr)): return self.N v=self.e while True: while L%2==0: L>>=1 vv=self.f(v,self.segment_tree[L]) if f(vv): v=vv L+=1 else: while L>=1 r>>=1 if f(self.f(vl,vr)): return 0 v=self.e while True: R-=1 while R>1 and R%2: R>>=1 vv=self.f(self.segment_tree[R],v) if f(vv): v=vv else: while R1: i>>=1 idxN.append(i) while j>1: j>>=1 idxM.append(j) i=idxN[0] for j in idxM[1:]: self.segment_tree[i][j]=self.f(self.segment_tree[i][j<<1],self.segment_tree[i][j<<1|1]) for i in idxN[1:]: for j in idxM: self.segment_tree[i][j]=self.f(self.segment_tree[i<<1][j],self.segment_tree[i<<1|1][j]) def Build(self,lst): assert len(lst)<=self.N assert all(len(lst[i] for i in range(self.N))<=self.M) for i in range(len(lst)): for j in range(len(lst[i])): self.segment_tree[i+self.N][j+self.M]=lst[i][j] for i in range(self.N-1,0,-1): for j in range(2*self.M-1,self.M-1,-1): self.segment_tree[i][j]=self.f(self.segment_tree[i<<1][j],self.segment_tree[i<<1|1][j]) for i in range(2*self.N-1,-1,-1): for j in range(self.M-1,0,-1): self.segment_tree[i][j]=self.f(self.segment_tree[i][j<<1],self.segment_tree[i][j<<1|1]) def Fold(self,LN=None,RN=None,LM=None,RM=None): LN+=self.N RN+=self.N LM+=self.M RM+=self.M idxN=[] idxM=[] while LN>=1 RN>>=1 while LM>=1 RM>>=1 retu=self.e for i in idxN: for j in idxM: retu=self.f(retu,self.segment_tree[i][j]) return retu def Fold_Index(self,LN=None,RN=None,LM=None,RM=None): if LN==None: LN=self.N else: LN+=self.N if RN==None: RN=self.N*2 else: RN+=self.N if LM==None: LM=self.M else: LM+=self.M if RM==None: RM=self.M*2 else: RM+=self.M if LN==RN and LM==RM: return None idxN=[] idxM=[] while LN>=1 RN>>=1 while LM>=1 RM>>=1 v=self.e for i in idxN: for j in idxM: v=self.f(v,self.segment_tree[i][j]) for i in idxN: for j in idxM: if v==self.f(v,self.segment_tree[i][j]): break else: continue break while i>h) def Operate_Range(self,a,L=None,R=None): if L==None: L=self.N else: L+=self.N if R==None: R=self.N*2 else: R+=self.N L0=L//(L&-L) R0=R//(R&-R)-1 self.Propagate_Above(L0) self.Propagate_Above(R0) while L>=1 R>>=1 def Update(self): for i in range(1,self.N): self.Propagate_At(i) self.segment_tree_act[i]=self.e_act def __str__(self): import copy segment_tree_act=copy.deepcopy(self.segment_tree_act) for i in range(1,self.N): segment_tree_act[i<<1]=self.f_act(segment_tree_act[i<<1],segment_tree_act[i]) segment_tree_act[i<<1|1]=self.f_act(segment_tree_act[i<<1|1],segment_tree_act[i]) segment_tree_act[i]=self.e_act segment_tree_act[i]=self.e_act return "["+", ".join(map(str,[self.operate(x,a) for x,a in zip(self.lst,segment_tree_act[self.N:])]))+"]" def __repr__(self): return "Dual_Segment_Tree("+str(self)+")" class Lazy_Segment_Tree: def __init__(self,N,f,e,f_act,e_act,operate,lst=None,bisect_search=True): self.N=N self.f=f self.e=e self.f_act=f_act self.e_act=e_act self.operate=operate self.bisect_search=bisect_search if self.bisect_search: self.le=1 while self.le>h) def Recalculate_Above(self,i): while i>1: i>>=1 self.segment_tree[i]=self.f(self.Operate_At(i<<1),self.Operate_At(i<<1|1)) def Build(self,lst): for i,x in enumerate(lst): self.segment_tree[i+self.le]=x for i in range(self.le-1,0,-1): self.segment_tree[i]=self.f(self.segment_tree[i<<1],self.segment_tree[i<<1|1]) self.segment_tree_act=[self.e_act]*(self.le+self.le) def Fold(self,L=None,R=None): if L==None: L=self.le else: assert 0<=L<=self.le L+=self.le if R==None: R=self.le*2 else: assert 0<=R<=self.le R+=self.le self.Propagate_Above(L//(L&-L)) self.Propagate_Above(R//(R&-R)-1) vL=self.e vR=self.e while L>=1 R>>=1 return self.f(vL,vR) def Fold_Index(self,L=None,R=None): if L==None: L=self.le else: assert 0<=L<=self.le L+=self.le if R==None: R=self.le*2 else: assert 0<=R<=self.le R+=self.le if L==R: return None x=self.Fold(L-self.le,R-self.le) while L>=1 R>>=1 while i>=1 R>>=1 self.Recalculate_Above(L0) self.Recalculate_Above(R0) def Update(self): for i in range(1,self.le): self.Propagate_At(i) for i in range(self.le,self.le*2): self.segment_tree[i]=self.Operate_At(i) self.segment_tree_act[i]=self.e_act for i in range(self.le-1,0,-1): self.segment_tree[i]=self.f(self.segment_tree[i<<1],self.segment_tree[i<<1|1]) def Bisect_Right(self,L=None,f=None): assert self.bisect_search if L==self.le: return self.le if L==None: L=0 assert 0<=L<=self.le L+=self.le self.Propagate_Above(L//(L&-L)) self.Propagate_Above(self.le//(self.le&-self.le)-1) l,r=L,self.le*2 vl=self.e vr=self.e while l>=1 r>>=1 if f(self.f(vl,vr)): return self.N v=self.e self.Propagate_Above(L) while True: while L%2==0: L>>=1 vv=self.f(v,self.Operate_At(L)) if f(vv): v=vv L+=1 else: while L>=1 r>>=1 if f(self.f(vl,vr)): return 0 v=self.e self.Propagate_Above(R-1) while True: R-=1 while R>1 and R%2: R>>=1 vv=self.f(self.Operate_At(R),v) if f(vv): v=vv else: while R>=1 R>>=1 def __call__(self,x): i=self.idx[x]+self.N retu=self.inf while i: a,b=self.li_chao_tree[i] retu=min(retu,a*x+b) i>>=1 return retu def __getitem__(self,i): x=self.X[i] i+=self.N retu=self.inf while i: a,b=self.li_chao_tree[i] retu=min(retu,a*x+b) i>>=1 return retu def __str__(self): li_chao_tree=[(0,self.inf)]*self.N for i,x in enumerate(self.X): ii=i+self.N while ii: aa,bb=self.li_chao_tree[ii] a,b=li_chao_tree[i] if aa*x+bb>=1 return "["+", ".join(map(str,li_chao_tree))+"]" def __repr__(self): return "Li_Chao_Tree("+str(self)+")" N=int(input()) X,Y=[],[] for i in range(N): x,y=map(int,input().split()) X.append(x) Y.append(y) M=int(input()) Z,W=[],[] for i in range(M): z,w=map(int,input().split()) Z.append(z) W.append(w) inf=1<<30 ans=inf for t in range(4): if t%2==0: ma=max(X+Z) X=[ma-x for x in X] Z=[ma-z for z in Z] N,M=M,N X,Y,Z,W=Z,W,X,Y leX=max(X+Z)+1 leY=max(Y+W)+1 ST=Segment_Tree(leY,max,-inf) query0=[[] for x in range(leX)] query1=[[] for x in range(leX)] for i in range(N): query0[X[i]].append(Y[i]) for i in range(M): query1[Z[i]].append(W[i]) for x in range(leX): for y in query0[x]: ST[y]=max(ST[y],x+y) for y in query1[x]: ans=min(ans,x+y-ST.Fold(0,y+1)) print(ans)