#include<iostream>
#include<vector>
#include<map>
#include<queue>
#include<cassert>
#include<algorithm>
using namespace std;
typedef long long ll;
const int N = 1e5 + 10;
struct Edge {
int v, w, next;
} pool[2 * N];
int ne, head[N];
inline void addEdge(int u, int v, int w) {
pool[++ne] = {v, w, head[u]};
head[u] = ne;
}
struct Op {
int t, v;
inline bool operator<(const Op &b) const {
if(t != b.t) return t < b.t;
return v > b.v;
}
inline bool operator==(const Op &b) const {
return t == b.t;
}
};
struct myPair {
ll p, v;
inline bool operator<(const myPair &b) const {
return v > b.v;
}
};
int n, m;
ll dep[N];
vector<Op> op[N];
map<ll, ll> mp1[N], mp2[N]; ll f[N];
priority_queue<myPair> que[N];
void update(int id, ll v) {
if(v && !que[id].empty()) assert(que[id].top().v > f[id]);
while(!que[id].empty() && que[id].top().v <= f[id] + v) {
myPair u = que[id].top(); que[id].pop();
auto tmp = mp1[id].lower_bound(u.p); if(tmp == mp1[id].end() || tmp->first != u.p) continue; ll &l = tmp->second;
tmp = mp2[id].lower_bound(u.p + u.v); if(tmp == mp2[id].end() || tmp->first != u.p + u.v) continue; ll &r = tmp->second;
if(-l < r) {
r += l, l = 0, mp1[id].erase(u.p);
auto it = mp1[id].upper_bound(u.p + u.v - f[id]);
if(it != mp1[id].begin()) {
--it; assert(it->first < u.p);
que[id].push({it->first, u.p + u.v - it->first});
}
} else if(-l > r) {
l += r, r = 0, mp2[id].erase(u.p + u.v);
auto it = mp2[id].lower_bound(u.p + f[id]);
if(it != mp2[id].end()) {
assert(it->first > u.p + u.v);
que[id].push({u.p, it->first - u.p});
}
} else {
l = r = 0, mp1[id].erase(u.p), mp2[id].erase(u.p + u.v);
auto it1 = mp1[id].upper_bound(u.p + u.v - f[id]);
auto it2 = mp2[id].lower_bound(u.p + f[id]);
if(it1 != mp1[id].begin() && it2 != mp2[id].end()) {
--it1; assert(it1->first < u.p), assert(it2->first > u.p + u.v);
que[id].push({it1->first, it2->first - it1->first});
}
}
}
f[id] += v;
}
void insert(int id, ll p, ll v) {
if(v == 0) return;
if(v > 0) {
auto it = mp1[id].upper_bound(p - f[id]);
if(it != mp1[id].begin()) {
--it;
que[id].push({it->first, p - it->first});
}
mp2[id][p] += v;
} else {
auto it = mp2[id].lower_bound(p);
p -= f[id];
if(it != mp2[id].end()) {
que[id].push({p, it->first - p});
}
mp1[id][p] += v;
}
update(id, 0);
}
void merge(int x, int y) {
if(mp1[x].size() + mp2[x].size() < mp1[y].size() + mp2[y].size()) {
merge(y, x);
swap(que[x], que[y]); swap(mp1[x], mp1[y]), swap(mp2[x], mp2[y]), swap(f[x], f[y]);
return;
}
for(auto [pos, val] : mp1[y]) insert(x, pos + f[y], val);
for(auto [pos, val] : mp2[y]) insert(x, pos, val);
map<ll, ll>().swap(mp1[y]), map<ll, ll>().swap(mp2[y]), priority_queue<myPair>().swap(que[y]);
}
inline ll query(int id, ll p) {
auto it = mp1[id].find(p - f[id]);
if(it != mp1[id].end()) return it->second;
it = mp2[id].find(p);
if(it != mp2[id].end()) return it->second;
return 0;
}
myPair buf[N]; int top;
void dfs(int u, int fa) {
for(int e = head[u]; e; e = pool[e].next) {
int v = pool[e].v, w = pool[e].w;
if(v == fa) continue;
dep[v] = dep[u] + w;
dfs(v, u);
for(Op &o : op[v]) {
ll tmp = 2 * (o.t - dep[v]) + 1;
buf[++top] = {tmp, max(0ll, o.v - max(0ll, max(-query(v, tmp - 1), query(v, tmp))))};
}
update(v, 2);
while(top) insert(v, buf[top].p, buf[top].v), insert(v, buf[top].p + 1, -buf[top].v), top--;
update(v, 4ll * w - 2);
merge(u, v);
}
}
int main() {
freopen("cake.in", "r", stdin);
freopen("cake.out", "w", stdout);
ios::sync_with_stdio(0);
cin.tie(0);
cin >> n;
for(int i = 1; i <= n - 1; i++) {
int u, v, w; cin >> u >> v >> w;
addEdge(u, v, w), addEdge(v, u, w);
}
addEdge(0, 1, 1), addEdge(1, 0, 1);
cin >> m;
for(int i = 1; i <= m; i++) {
int t, p, v; cin >> t >> v >> p;
op[p].push_back({t, v});
}
for(int i = 1; i <= n; i++) sort(op[i].begin(), op[i].end()), op[i].resize(unique(op[i].begin(), op[i].end()) - op[i].begin());
dfs(0, 0);
ll ans = 0, cur = 0;
for(auto i = mp1[0].begin(), j = mp2[0].begin(); i != mp1[0].end() || j != mp2[0].end(); ) {
if(i != mp1[0].end() && (j == mp2[0].end() || i->first + f[0] < j->first)) cur += i->second, i++;
else cur += j->second, j++;
ans = max(ans, cur);
}
cout << ans << '\n';
return 0;
}