#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;
REP(i, 1, n) {
cin>>a[i][0]>>a[i][1]>>a[i][2];
R += a[i][0];
}
int m; cin>>m;
ll L = 0;
REP(j, 0, m-1) {
array<ll, 3> b;
cin>>b[0]>>b[1]>>b[2];
b[0]*=-1;
b[2]*=-1;
L += b[0];
a.push_back(b);
}
const ll INF = 1e18;
vector<map<ll, ll>> dp(n+m+1);
dp[0][0] = 0;
REP(i, 1, n+m){
REP(x, L, R){
if (dp[i].find(x) == dp[i].end()) dp[i][x] = -INF;
if (dp[i-1].find(x) == dp[i-1].end()) dp[i-1][x] = -INF;
if (dp[i-1].find(x-a[i][0]) == dp[i-1].end()) dp[i-1][x-a[i][0]] = -INF;
dp[i][x] = max(dp[i-1][x], dp[i-1][x-a[i][0]] - a[i][2]);
}
}
ll best = 0;
REP(x, 0, R) best = max(best, dp[n+m][x]);
cout << best << endl;
}
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
int tt=1;
// cin>>tt;
while(tt--) solve();
}