#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define int ll
using P = pair<int, int>;
#define all(x) x.begin(), x.end()
#define rep(x,s,e) for (auto x=(s)-((s)>(e));x!=(e)-((s)>(e));((s)<(e)?x++:x--))
#define sz(x) (int)x.size()
const char nl = '\n';
const int N = 1e5+10;
const int inf = 1e9*2e3+100;
struct nd {
int c, f, v;
};
bool cmp(const nd &a, const nd &b) {
return a.f > b.f;
}
void solve() {
int n; cin >> n;
vector<nd> a(n);
rep(i, 0, n) {
cin >> a[i].c >> a[i].f >> a[i].v;
a[i].v *= -1;
}
int m; cin >> m;
rep(i, n, n+m-1) {
nd p;
cin >> p.c >> p.f >> p.v;
p.c *= -1; a.push_back(p);
}
sort(all(a), cmp);
vector<int> dp(N, -inf);
dp[0] = 0;
rep(i, 0, n+m-1) {
if (a[i].c>0)rep(j, N, a[i].c)dp[j] = max(dp[j], dp[j-a[i].c]+a[i].v);
else rep(j, 0, N+a[i].c)dp[j] = max(dp[j], dp[j-a[i].c]+a[i].v);
}
int res = 0;
rep(i, 0, N)
res = max(res, dp[i]);
cout << res << nl;
}
int32_t main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
solve();
return 0;
}