#include using namespace std; using ll = long long; using ull = unsigned long long; using ld = long double; template using vc = vector; template using vvc = vc>; using pi = pair; using pl = pair; using vi = vc; using vvi = vvc; using vl = vc; using vvl = vvc; #define rep(i,a,b) for (int i = (int)(a); i < (int)(b); i++) #define irep(i,a,b) for (int i = (int)(a); i > (int)(b); i--) #define all(a) a.begin(),a.end() #define print(n) cout << n << '\n' #define pritn(n) print(n) #define printv(n,a) {copy(all(n),ostream_iterator(cout," ")); cout<<"\n";} #define printvv(n,a) {for(auto itr:n) printv(itr,a);} #define rup(a,b) (a+b-1)/b #define input(A,N) rep(i,0,N) cin>>A[i] #define chmax(a,b) a = max(a,b) #define chmin(a,b) a = min(a,b) template< typename T > struct edge{ int from,to,rev; T cost; /* *to:繋がってる先 *cost:辺のコスト */ edge(int from,int to,int rev,T cost):from(from),to(to),rev(rev),cost(cost){}; edge &operator=(const int& x){ to = x; return *this; } }; template< typename T > struct graph{ int n; vector>> e; graph(int n):n(n),e(n){} void addedge(int from,int to,T cost){ e[from].emplace_back(from,to,(int)e[to].size(),cost); e[to].emplace_back(to,from,(int)e[from].size()-1,0); } vector> &operator[](int i) { return e[i]; } }; template< typename T > struct FordFulkerson{ const int INF = (T)1e9; vector vis; FordFulkerson(){}; T dfs(graph&g,int ni,int t,T f){ if(ni==t) return f; vis[ni] = 1; for(auto &e:g[ni]){ if(vis[e.to]||e.cost<=0) continue; T d = dfs(g,e.to,t,min(f,e.cost)); if(d>0){ e.cost -= d; g[e.to][e.rev].cost += d; return d; } } return 0; } //start:s->to:t T max_flow(graph&g,int s,int t){ T flow = 0; while(1){ vis.assign(g.n,0); T d = dfs(g,s,t,INF); if(d==0){ return flow; }else{ flow += d; } } return 0; } }; int main(){ cout << fixed << setprecision(15); int n; cin>>n; graph g(n+2); int now = 0; rep(i,0,n){ int b,c; cin>>b>>c; if(b>c){ now += b; b -= c; g.addedge(i,n+1,b); }else{ now += c; c -= b; g.addedge(n,i,c); } } int m; cin>>m; int inf = (int)1e9; rep(i,0,m){ int a,b; cin>>a>>b; g.addedge(b,a,inf); } FordFulkerson f; print(now-f.max_flow(g,n,n+1)); //system("pause"); return 0; }