#include using namespace std; int INF = numeric_limits::max() / 10; template class Dinic { const T INF; struct edge { int to; T cap; int rev; }; vector> G; vector min_cost, iter; bool bfs(int s, int t) { min_cost.assign(G.size(), -1); queue que; min_cost[s] = 0; que.push(s); while (!que.empty() && min_cost[t] == -1) { int p = que.front(); que.pop(); for (auto& e : G[p]) { if (e.cap > 0 && min_cost[e.to] == -1) { min_cost[e.to] = min_cost[p] + 1; que.push(e.to); } } } return min_cost[t] != -1; } T dfs(int idx, const int t, T flow) { if (idx == t) return flow; for (int& i = iter[idx]; i < (int)G[idx].size(); i++) { edge& e = G[idx][i]; if (e.cap > 0 && min_cost[idx] < min_cost[e.to]) { T d = dfs(e.to, t, min(flow, e.cap)); if (d > 0) { e.cap -= d; G[e.to][e.rev].cap += d; return d; } } } return 0; } // public: Dinic(int V) : INF(numeric_limits::max() / T{10}), G(V) {} void add_edge(int from, int to, T cap) { G[from].emplace_back((edge){to, cap, (int)G[to].size()}); G[to].emplace_back((edge){from, 0, (int)G[from].size() - 1}); } T max_flow(int s, int t) { T flow = 0; while (bfs(s, t)) { iter.assign(G.size(), 0); T f = 0; while ((f = dfs(s, t, INF)) > 0) flow += f; } return flow; } }; int main() { int N; cin >> N; Dinic dinic(100); int ans = 0; for (int i = 0; i < N; i++) { int B, C; cin >> B >> C; ans += B + C; dinic.add_edge(90, i, C); dinic.add_edge(i + 50, 95, B); dinic.add_edge(i, i + 50, INF); } int M; cin >> M; while (M--) { int D, E; cin >> D >> E; dinic.add_edge(E, D + 50, INF); } ans -= dinic.max_flow(90, 95); cout << ans << endl; }