#define _USE_MATH_DEFINES #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; typedef long long ll; typedef unsigned long long ull; typedef pair i_i; typedef pair ll_i; typedef pair d_i; typedef pair ll_ll; typedef pair d_d; struct edge { int u, v, y, m; }; ll MOD = 1000000007; ll _MOD = 1000000009; double EPS = 1e-10; int f(int N, vector& a, vector& B, vector& C) { int sum = 0; for (int i = 0; i < N; i++) if (a[i] == 1) sum += C[i]; else if (a[i] == 2) sum += B[i]; return sum; } int main() { int N; cin >> N; vector B(N), C(N); for (int i = 0; i < N; i++) cin >> B[i] >> C[i]; int M; cin >> M; vector p(M); for (int j = 0; j < M; j++) cin >> p[j].first >> p[j].second; sort(p.begin(), p.end()); vector D(M), E(M); for (int j = 0; j < M; j++) { D[j] = p[j].first; E[j] = p[j].second; } vector a(N); random_device rd; mt19937 mt(rd()); int ans = 0; for (int x = 0; x < 59049; x++) { int y = x; for (int i = 0; i < min(10, N); i++) { a[i] = y % 3; y /= 3; } int z = 0, maxi = 0; for (int t = 99; t >= 0; t--) { vector _a = a; if (N > 10) { int i = 10 + mt() % (N - 10); if (mt() % 2) _a[i] = (_a[i] + 1) % 3; else _a[i] = (_a[i] + 4) % 3; } for (int j = 0; j < M; j++) if (_a[D[j]] == 2 && _a[E[j]] == 1) _a[E[j]] = 2; int _z = f(N, _a, B, C); if (_z > x - t / 10) { z = _z; a = _a; } maxi = max(maxi, z); } ans = max(ans, maxi); } cout << ans << endl; }