#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 _2d_segtree { vector> seg; _2d_segtree() : _2d_segtree(0, 0) { } /* compressed_2d_segtree(vector tx, vector v){ _tx = tx; seg = segtree(v); } */ _2d_segtree(int h,int w){ if(h>w){ swap(h,w); F = true; } int n = h; log = 0; while((1LL<(w)); } void set(int px, int py, S x) { if(F)swap(px,py); px += size; seg[px].set(py,x); for (int i = 1; i <= log; i++) update(px >> i, py); } S get(int px,int py) { if(F)swap(px,py); return seg[px+size].get(py); } S prod(int lx,int rx,int ly,int ry) { if(F){ swap(lx,ly); swap(rx,ry); } S sml = e(), smr = e(); lx += size; 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; bool F = false; void update(int k, int 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 (long long)Inf64; } int main(){ int n; cin>>n; vector> tp(n); rep(i,n){ cin>>tp[i].first>>tp[i].second; } _2d_segtree s00(1000,1000),s01(1000,1000),s10(1000,1000),s11(1000,1000); 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,1000,y,1000)-x-y); ans = min(ans,s01.prod(x,1000,0,y)-x+y); ans = min(ans,s10.prod(0,x,y,1000)+x-y); ans = min(ans,s11.prod(0,x,0,y)+x+y); } cout<