#include<bits/stdc++.h>
using namespace std;
#define REP(i, from, to) for (ll i=(from);i<=(to);++i)
#define PER(i, from, to) for (ll i=(from);i>=(to);--i)
#define ALL(x) (x).begin(), (x).end()
#define SZ(x) (ll)(x).size()
#define PB push_back
#define MP make_pair
#define F first
#define S second
typedef long long ll;
typedef pair<ll, ll> pll;
typedef vector<ll> vll;
void solve(){
int n; cin>>n;
vector<array<ll, 3>> a(n+1);
int R = 0, L = 0;
REP(i, 1, n) {
cin>>a[i][0]>>a[i][1]>>a[i][2];
R += a[i][0];
}
int m; cin>>m;
REP(j, 0, m-1) {
array<ll, 3> b;
cin>>b[0]>>b[1]>>b[2];
L += b[0];
b[0]*=-1;
b[2]*=-1;
a.push_back(b);
}
const ll INF = 1e18;
vector<vll> dp(n+m+1, vll(L+R+1, -INF));
dp[0][0+L] = 0;
REP(i, 1, n+m){
REP(x, -L, R){
dp[i][x+L] = max(dp[i][x+L], dp[i-1][x+L]);
if (x-a[i][0] < -L) continue;
if (x-a[i][0] > R) continue;
dp[i][x+L] = max(dp[i][x+L], dp[i-1][x-a[i][0]+L] - a[i][2]);
}
}
ll best = 0;
REP(x, 0, R) best = max(best, dp[n+m][x+L]);
cout << best << endl;
}
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
int tt=1;
// cin>>tt;
while(tt--) solve();
}