#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vll = vector<ll>;
using vvl = vector<vll>;
using pll = pair<ll,ll>;
using vpl = vector<pll>;
using vvp = vector<vpl>;
#define f first
#define s second
#define pb push_back
#define all(v) v.begin(),v.end()
int main(){
ios_base::sync_with_stdio(0);cin.tie(NULL);
ll n, m;
cin >> n;
vector<tuple<ll,ll,ll>> ord;// freq, amount, price
for(ll i = 0; i < n; ++i){
ll a, b, c;
cin >> a >> b >> c;
ord.pb({b, a, -c});
}
cin >> m;
for(ll i = 0; i < m; ++i){
ll a, b, c;
cin >> a >> b >> c;
ord.pb({b, -a, c});
}
sort(all(ord),greater<tuple<ll,ll,ll>>());
vvl dp(n+m+1, vll(max(n,m)*50+1, -1e14));
dp[0][0]=0;
for(ll i = 1; i <= n+m; ++i){
auto [f, am, pr] = ord[i-1];
for(ll j = 0; j < dp[0].size(); ++j){
dp[i][j]=dp[i-1][j];
if(j>190){
cout << "";
}
if(j>=am && j-am<dp[0].size())dp[i][j] = max(dp[i][j], dp[i-1][j-am]+pr);
}
}
cout << *max_element(all(dp[n+m]));
}