#define rep(i,n) for(int i=0;i<(int)(n);i++) #define ALL(v) v.begin(),v.end() typedef long long ll; #include #include using namespace std; using namespace atcoder; template using V=vector; template using VV=V>; //B(n,V(n)) const ll INF=1e18; int main(){ ios::sync_with_stdio(false); std::cin.tie(nullptr); int n; cin>>n; ll sum=0; V B(n),C(n),MA(n); rep(i,n){ cin>>B[i]>>C[i]; MA[i]=B[i]+C[i]; sum+=MA[i]; } int m; cin>>m; V D(m),E(m); rep(i,m) cin>>D[i]>>E[i]; mf_graph graph(2*n+2); int s=2*n,t=2*n+1; rep(i,n) graph.add_edge(s,i,MA[i]-C[i]); rep(i,n) graph.add_edge(i,i+n,MA[i]); rep(i,n) graph.add_edge(i+n,t,MA[i]-B[i]); rep(i,m) graph.add_edge(D[i],E[i]+n,INF); cout<