#include #include #include using namespace std; using namespace atcoder; using mint = modint998244353; #define rep(i,n) for (int i = 0; i < (n); ++i) #define Inf32 1000000001 #define Inf64 4000000000000000001LL template struct compressed_segtree { segtree seg; compressed_segtree() : compressed_segtree(vector(0)) { } compressed_segtree(vector tx, vector v){ _tx = tx; seg = segtree(v); } compressed_segtree(vector tx){ _tx = tx; seg = segtree(tx.size()); } int lower_ind(long long p){ return distance(_tx.begin(),lower_bound(_tx.begin(),_tx.end(),p)); } void set(long long p, S x) { seg.set(lower_ind(p), x); } S get(long long p) { int idx = lower_ind(p); if(idx!=_tx.size()&&_tx[idx]==p)return seg.get(idx); return e(); } S prod(long long l,long long r) { return seg.prod(lower_ind(l),lower_ind(r)); } S all_prod() { return seg.all_prod(); } vector _tx; }; template struct compressed_2d_segtree { vector> seg; compressed_2d_segtree() : compressed_2d_segtree(vector(0)) { } /* compressed_2d_segtree(vector tx, vector v){ _tx = tx; seg = segtree(v); } */ compressed_2d_segtree(vector> tp){ _tp = tp; rep(i,tp.size()){ _tx.push_back(tp[i].first); _ty.push_back(tp[i].second); } sort(_tx.begin(),_tx.end()); _tx.erase(unique(_tx.begin(),_tx.end()),_tx.end()); sort(_ty.begin(),_ty.end()); _ty.erase(unique(_ty.begin(),_ty.end()),_ty.end()); int n = _tx.size(); log = 0; while((1LL<> ys(size*2); rep(i,tp.size()){ ys[size + lower_indx(tp[i].first)].push_back(tp[i].second); } for(int i=size;i(ys[i]); } for(int i=size-1;i>=1;i--){ vector a(ys[i*2].rbegin(),ys[i*2].rend()), b(ys[i*2+1].rbegin(),ys[i*2+1].rend()); while(a.size()>0||b.size()>0){ if(a.size()==0){ swap(a,b); } else if(b.size()>0){ if(a.back()>b.back())swap(a,b); } if(ys[i].size()==0||ys[i].back()!=a.back())ys[i].push_back(a.back()); a.pop_back(); } seg[i] = compressed_segtree(ys[i]); ys[i*2].clear(); ys[i*2+1].clear(); } } int lower_indx(long long p){ return distance(_tx.begin(),lower_bound(_tx.begin(),_tx.end(),p)); } int lower_indy(long long p){ return distance(_ty.begin(),lower_bound(_ty.begin(),_ty.end(),p)); } void set(long long px, long long py, S x) { px = lower_indx(px) + size; seg[px].set(py,x); for (int i = 1; i <= log; i++) update(px >> i, py); } S get(long long px, long long py) { int idx = lower_indx(px); if(idx!=_tx.size()&&_tx[idx]==px)return seg[idx+size].get(py); return e(); } S prod(long long lx,long long rx, long long ly, long long ry) { S sml = e(), smr = e(); lx = lower_indx(lx)+size; rx = lower_indx(rx)+size; while (lx < rx) { if (lx & 1) sml = op(sml, seg[lx++].prod(ly,ry)); if (rx & 1) smr = op(seg[--rx].prod(ly,ry), smr); lx >>= 1; rx >>= 1; } return op(sml, smr); } S all_prod() { return seg[1].all_prod(); } int size, log; vector> _tp; vector _tx,_ty; void update(int k, long long py) { seg[k].set(py, op(seg[2*k].get(py), seg[2*k+1].get(py))); } }; long long op(long long a,long long b){ return min(a,b); } long long e(){ return Inf64; } int main(){ int n; cin>>n; vector> tp(n); rep(i,n){ cin>>tp[i].first>>tp[i].second; } compressed_2d_segtree s00(tp),s01(tp),s10(tp),s11(tp); rep(i,n){ s00.set(tp[i].first,tp[i].second,tp[i].first+tp[i].second); s01.set(tp[i].first,tp[i].second,tp[i].first-tp[i].second); s10.set(tp[i].first,tp[i].second,-tp[i].first+tp[i].second); s11.set(tp[i].first,tp[i].second,-tp[i].first-tp[i].second); } int m; cin>>m; long long ans = Inf64; rep(i,m){ long long x,y; cin>>x>>y; ans = min(ans,s00.prod(x,Inf64,y,Inf64)-x-y); ans = min(ans,s01.prod(x,Inf64,0,y)-x+y); ans = min(ans,s10.prod(0,x,y,Inf64)+x-y); ans = min(ans,s11.prod(0,x,0,y)+x+y); } cout<