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