#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 = 7e4+10;
const int inf = 1e9*2e3+100;
void solve() {
int n; cin >> n;
vector<P> a(n);
for (auto &i: a) {
int x;
cin >> i.first >> x >> i.second;
}
vector<int> dp1(N, 0);
for (auto i: a)
rep(j, N, i.first)
dp1[j] = max(dp1[j], dp1[j-i.first]+i.second);
int m; cin >> m;
vector<P> b(m);
for (auto &i: b) {
int x;
cin >> i.first >> x >> i.second;
}
vector<int> dp2(N, inf);
dp2[0] = 0;
for (auto i: b)
rep(j, N, i.first)
dp2[j] = min(dp2[j], dp2[j-i.first]+i.second);
int res = 0;
rep(i, 1, N)
rep(j, 1, i+1)
res = max(res, dp1[i]-dp2[j]);
cout << res << nl;
}
int32_t main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
solve();
return 0;
}