#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 x = 0, maxi = 0; for (int t = 0; t < 2000000; t++) { vector _a = a; int i = t % N; //mt() % N; if (mt() % 2) _a[i] = (_a[i] + 1) % 3; else _a[i] = (_a[i] - 1) % 3; for (int j = 0; j < M; j++) if (_a[D[j]] == 2 && _a[E[j]] == 1) _a[E[j]] = 2; int _x = f(N, _a, B, C); if (_x > x - (2000000 - t) / 100000) { x = _x; a = _a; } maxi = max(maxi, x); } cout << maxi << endl; }