#include #include using namespace std; using namespace atcoder; #define rep(i, n) REP(i, 0, n) #define REP(i, s, e) for (int i = (s); i < (int)(e); i++) #define repr(i, n) REPR(i, n, 0) #define REPR(i, s, e) for (int i = (int)(s - 1); i >= (int)(e); i--) #define all(r) r.begin(), r.end() #define rall(r) r.rbegin(), r.rend() typedef long long ll; typedef vector vi; typedef vector vl; template T chmax(T& a, const U& b) { if (a >= b) return false; a = b; return true; } template T chmin(T& a, const U& b) { if (a <= b) return false; a = b; return true; } void yes_no(bool f, string yes = "Yes", string no = "No") { cout << (f ? yes : no) << "\n"; } void solve() { int n; cin >> n; const int sz = n + n + 2; mf_graph g(sz); int s = sz - 2, t = sz - 1; ll sum = 0; const ll inf = 1e18; rep(i, n) { int a, b; cin >> a >> b; g.add_edge(s, i, a); g.add_edge(n + i, t, b); g.add_edge(i, n + i, inf); sum += a + b; } int m; cin >> m; rep(i, m) { int a, b; cin >> a >> b; g.add_edge(a, n + b, inf); } ll flow = g.flow(s, t); ll ans = sum - flow; cout << ans << "\n"; } int main() { cin.tie(0); ios::sync_with_stdio(false); int t = 1; // multi-testcase // cin >> t; rep(ti, t) solve(); return 0; }