#include using namespace std; typedef long long ll; typedef pair P; #define For(i, a, b) for(int i = (a); i < (b); i++) #define Rep(i, n) For(i, 0, (n)) #define Rrep(i, n) for(int i = (n - 1); i >= 0; i--) #define Itr(i, c) for(typeof(c.begin()) i = c.begin(); i != c.end(); i++) #define pb push_back const int inf = 999999999; const int mod = 1000000007; const int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1}; typedef struct{ int to, cap, rev; }edge; vector G[82]; bool used[82]; void add_edge(int from, int to, int cap){ int p = G[to].size(), q = G[from].size(); G[from].pb((edge){to, cap, p}); G[to].pb((edge){from, 0, q}); } int dfs(int v, int t, int f){ if(v == t) return f; used[v] = true; for(int i = 0; i < G[v].size(); i++){ edge &e = G[v][i]; if(!used[e.to] && e.cap > 0){ int d = dfs(e.to, t, min(f, e.cap)); if(d > 0){ e.cap -= d; G[e.to][e.rev].cap += d; return d; } } } return 0; } int max_flow(int s, int t){ int flow = 0; while(1){ Rep(i, sizeof(used)/sizeof(*used)){ used[i] = false; } int f = dfs(s, t, inf); if(f == 0) return flow; flow += f; } } int main(){ int n, m, sum = 0; cin >> n; Rep(i, n){ int b, c; cin >> b >> c; sum += max(b, c); add_edge(80, i * 2, max(b, c) - c); add_edge(i * 2 + 1, 81, max(b, c) - b); add_edge(i * 2, i * 2 + 1, max(b, c)); } cin >> m; Rep(i, m){ int d, e; cin >> d >> e; add_edge(d * 2 + 1, e * 2, inf); } int f = max_flow(80, 81); cout << sum - f << endl; return 0; }