#include <bits/stdc++.h>
#include <bits/extc++.h>
using namespace __gnu_pbds;
using namespace std;
// #pragma GCC optimize("Ofast")
// #pragma GCC optimize ("unroll-loops")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#define ff first
#define sc second
#define pb push_back
#define ll long long
#define pll pair<ll, ll>
#define pii pair<int, int>
#define ull unsigned long long
#define all(x) (x).begin(),(x).end()
#define rall(x) (x).rbegin(),(x).rend()
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
mt19937_64 rngl(chrono::steady_clock::now().time_since_epoch().count());
// #define int long long
// #define int unsigned long long
// #define ordered_set(T) tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>
// #define ordered_multiset(T) tree<T, null_type, less_equal<T>, rb_tree_tag, tree_order_statistics_node_update>
const ll mod = 1e9 + 7;
// const ll mod = 998244353;
const ll inf = 1e9;
const ll biginf = 1e18;
const int maxN = 2005;
const int maxC = 1e5 + 15;
bool comp(vector<int> a, vector<int> b) {
if (a[1] == b[1]) return a[0] > b[0];
return a[1] > b[1];
}
int n, m;
ll dp[maxC][2];
void solve() {
cin >> n;
vector<vector<int>> ord;
for (int i = 1; i <= n; i++) {
int c, f, v; cin >> c >> f >> v;
ord.pb({c, f, -v});
}
cin >> m;
for (int i = 1; i <= m; i++) {
int c, f, v; cin >> c >> f >> v;
ord.pb({-c, f, v});
}
sort(all(ord), comp);
for (int i = 0; i <= 50 * n; i++) dp[i][0] = -biginf;
dp[0][0] = 0;
for (auto _ : ord) {
int c = _[0], f = _[1], v = _[2];
for (int i = 0; i <= 50 * n; i++) dp[i][1] = dp[i][0];
for (int i = 0; i <= 50 * n; i++) {
if (dp[i][0] == -biginf) continue;
if (0 <= i + c) dp[i + c][1] = max(dp[i + c][1], dp[i][0] + v);
}
for (int i = 0; i <= 50 * n; i++) dp[i][0] = dp[i][1];
}
ll ans = 0;
for (int i = 0; i <= 50 * n; i++)
ans = max(ans, dp[i][0]);
cout << ans;
}
int32_t main() {
// #ifndef ONLINE_JUDGE
// freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
// #endif
ios_base::sync_with_stdio(false);
cin.tie(nullptr); cout.tie(nullptr);
int t = 1;
// cin >> t;
while (t--) {
solve();
cout << '\n';
}
return 0;
}