#include <bits/stdc++.h>
#define int long long
#define pii pair<int, int>
#define pb push_back
#define gcd __gcd
#define endl "\n"
#define task "hihi"
using namespace std;
const int N = 3e5 + 5, MOD = 1e9 + 7, INF = 1e18 + 5;
int n, m, dp[N], pre[N];
vector<array<int, 3>> a;
signed main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
//freopen(task".inp", "r", stdin);
//freopen(task".out", "w", stdout);
cin >> n;
for(int i = 1; i <= n; i++){
int x, y, z; cin >> x >> y >> z;
a.pb({x, y, -z});
}
cin >> m;
for(int i = 1; i <= m; i++){
int x, y, z; cin >> x >> y >> z;
a.pb({-x, y, z});
}
sort(a.begin(), a.end(), [](array<int, 3> x, array<int, 3> y){
return (x[1] == y[1] ? x[2] < y[2] : x[1] > y[1]);
});
fill(pre, pre + N, -INF);
fill(dp, dp + N, -INF);
pre[0] = 0;
int sum = 0, ans = 0;
for(int i = 0; i < a.size(); i++){
for(int j = max(0LL, a[i][0]); j <= sum + a[i][0]; j++){
if(pre[j - a[i][0]] == -INF) continue;
dp[j] = max(dp[j], pre[j - a[i][0]] + a[i][2]);
ans = max(ans, dp[j]);
}
sum = max(sum, sum + a[i][0]);
for(int j = max(0LL, a[i][0]); j <= sum; j++){
pre[j] = max(dp[j], pre[j]);
}
}
cout << ans;
return 0;
}