#include typedef long long LL; const int N = 40 + 7; const int P = 100 + 7; const int E = 10000 + 7; const int S = P - 1; const int T = P - 2; const int INF = 0x3f3f3f3f; int head[P], cur[P], nxt[E], to[E], fl[E], tot = 1; int n, m, ans; int d[P]; void add(int x, int y, int z, int w = 0) { nxt[++tot] = head[x], head[x] = tot, to[tot] = y, fl[tot] = z; nxt[++tot] = head[y], head[y] = tot, to[tot] = x, fl[tot] = w; } bool bfs() { std::queue q; memset(d, -1, sizeof d); d[S] = 0, q.push(S); while(!q.empty()) { int x = q.front(); q.pop(); for(int i = head[x]; i; i = nxt[i]) if(d[to[i]] == -1 && fl[i]) { d[to[i]] = d[x] + 1; q.push(to[i]); } } return d[T] != -1; } int dinic(int x, int y) { if(x == T) return y; int f = 0, t; for(int &i = cur[x]; i; i = nxt[i]) if(d[to[i]] == d[x] + 1 && fl[i]) { t = dinic(to[i], std::min(fl[i], y - f)); f += t; fl[i] -= t; fl[i ^ 1] += t; if(f == y) return y; } return f; } int main() { scanf("%d", &n); for(int i = 1, x, y; i <= n; ++i) { scanf("%d%d", &x, &y); ans += x + y; add(S, i, x); add(i, i + n, x + y); add(i + n, T, y); } scanf("%d", &m); for(int i = 1, x, y; i <= m; ++i) { scanf("%d%d", &x, &y); add(x + 1 + n, y + 1, INF); } while(bfs()) { memcpy(cur, head, sizeof cur); ans -= dinic(S, INF); } printf("%d\n", ans); return 0; }