#include <bits/stdc++.h>
using namespace std;
#define     el "\n"
#define     FOR(i,a,b) for(int i = (a), _b = (b); i <= _b; i++)
#define     FORD(i,a,b) for(int i = (a), _b = (b); i >= _b; i--)
#define     pb push_back
#define     fi first
#define     se second
#define     all(x) x.begin(),x.end()
#define     lg(x) __lg(x)
#define     alla(a,n) a+1,a+n+1
#define     ll long long
template <class T> bool maxi(T &x, T y) { if(x < y) { x = y ; return true ;} return false;}
template <class T> bool mini(T &x, T y) { if(x > y) { x = y ; return true ;} return false;}
const int N = 1e5 + 2;
int n, m;
vector<int> adj[N];
struct Edge
{
    int x, y, w;
} E[N];
void inp()
{
    cin >> n;
    for(int i = 1; i < n; i++) {
        int x, y; cin >> x >> y;
        adj[x].pb(y);
        adj[y].pb(x);
    }
    cin >> m;
    FOR(i, 1, m) {
        int x, y, w; cin >> x >> y >> w;
        E[i] = {x, y, w};
    }
}
/* Try your best
    No regrets */
    int h[N], up[N][21];
    int in[N], out[N], timer = 0;
    void dfs(int u, int p)
    {
        in[u] = ++timer;
        up[u][0] = p;
        FOR(j, 1, lg(n)) up[u][j] = up[up[u][j - 1]][j - 1];
        for(int v : adj[u]) if(v != p) {
            h[v] = h[u] + 1;
            dfs(v, u);
        }
        out[u] = timer;
    }
    int LCA(int u, int v)
    {
        if(h[u] < h[v]) swap(u, v);
        FORD(j, lg(n), 0) if(h[up[u][j]] >= h[v]) u = up[u][j];
        if(u == v) return u;
        FORD(j, lg(n), 0) if(up[u][j] != up[v][j]) {
            u = up[u][j];
            v = up[v][j];
        }
        return up[u][0];
    }
namespace subtask_1
{
    vector<int> G[N];
    int Val[N], temp[N];
    void dfsAns(int u, int p )
    {
        for(int v : G[u]) if(v != p)  {
            dfsAns(v, u);
            Val[u] += Val[v];
        }
    }
    void slv()
    {
        dfs(1, 1);
        ll res = 0;
        FOR(msk, 1, (1 << m) - 1) {
            vector<int> V;
            ll sum = 0;
            FOR(i, 0, m - 1) if(msk >> i & 1) {
                int x = E[i + 1].x, y = E[i + 1].y, w = E[i + 1].w;
                sum += w;
                V.pb(x);
                V.pb(y);
                Val[x]++;
                Val[y]++;
                int p =  LCA(x, y);
                Val[p] -= 2;
                temp[p]++;
            }
            sort(all(V), [](int x, int y) {
                return in[x] < in[y];
            });
            int Sz = V.size() - 1;
            for(int i = 1; i <= Sz; i++) {
                V.pb(LCA(V[i], V[i - 1]));
            }
            sort(all(V), [](int x, int y){
                return in[x] < in[y];
            });
            V.resize(unique(all(V)) - V.begin());
            deque<int> dq;
            for(int v : V) {
                while(!dq.empty() && out[dq.back()] < in[v]) dq.pop_back();
                if(dq.size()) {
                    G[dq.back()].pb(v);
                    G[v].pb(dq.back());
                }
                dq.push_back(v);
            }
            dfsAns(V[0], 0);
            bool hp = 1;
            for(int v : V) if(Val[v] + temp[v] > 1) hp = 0;
            if(hp) maxi(res, sum);
            for(int v : V) G[v].clear(), temp[v] = Val[v] = 0;
        }
        cout << res;
    }
}
namespace subtask_23
{
    bool check_sub()
    {
        FOR(i, 1, n) if(adj[i].size() > 2) return 0;
        return 1;
    }
    ll dp[N];
    ll bit[N];
    void upd(int x, ll val)
    {
        for(; x <= n; x += x & -x) maxi(bit[x], val);
    }
    ll get(int x)
    {
        ll ans = 0;
        for(; x >= 1; x -= x & -x) maxi(ans, bit[x]);
        return ans;
    }
    void slv()
    {
        FOR(i, 1, m) {
            if(E[i].x > E[i].y) swap(E[i].x, E[i].y);
        }
        sort(E + 1, E + m + 1, [](Edge a, Edge b) {
            return a.x < b.x;
        });
//        FOR(i, 1, m) cout << E[i].x << " " << E[i].y << " " << E[i].w << el; cout << el;
        dp[1] = E[1].w;
        upd(E[1].y, dp[1]);
        FOR(i, 2, m) {
            dp[i] = E[i].w;
            maxi(dp[i], get(E[i].x - 1) + E[i].w);
            upd(E[i].y, dp[i]);
        }
        cout << *max_element(alla(dp, m));
    }
}
namespace subtask_AC
{
    vector<int> qry[N];
    long long dp[N], temp[N];
    struct FenwickTree
    {
        long long bit[N];
        void upd(int x, ll val)
        {
            for(; x <= n; x += x & -x) bit[x] += val;
        }
        ll get(int x)
        {
            ll ans = 0;
            for(; x >= 1; x -= x & -x) ans += bit[x];
            return ans;
        }
        void update(int l, int r, ll val)
        {
            upd(l, val);
            upd(r + 1, -val);
        }
    } bit;
    void dfsAns(int u, int p )
    {
        for(int v : adj[u]) if(v != p) {
            dfsAns(v, u);
            temp[u] += dp[v];
        }
        dp[u] = temp[u];
        int lca = u;
        for(int idx : qry[lca]) {
            int u = E[idx].x, v = E[idx].y, w = E[idx].w;
            dp[lca] = max(dp[lca], temp[lca] + bit.get(in[u]) + bit.get(in[v]) + w);
        }
        bit.update(in[u], out[u], temp[u] - dp[u]);
    }
    void slv()
    {
        dfs(1, 1);
        FOR(i, 1, m) {
            int x = E[i].x, y = E[i].y;
            qry[LCA(x, y)].push_back(i);
        }
        dfsAns(1, 1);
        cout << dp[1];
    }
}
/* Code slowly, think carefully */
main()
{
    ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
    #define __Azul__ "election_campaign"
    if(fopen(__Azul__".inp", "r")) {
        freopen(__Azul__".inp", "r", stdin);
        freopen(__Azul__".out", "w", stdout);
    }
    bool qs = 0;
    int T = 1;
    if(qs) cin >> T;
    while(T--) {
        inp();
        return subtask_AC :: slv(), 0;
        if(m <= 15) subtask_1 :: slv();
        else if(subtask_23 :: check_sub()) subtask_23 :: slv();
        else subtask_AC :: slv();
    }
    cerr << "\nTime" << 0.001 * clock() << "s "; return 0;
}
Compilation message (stderr)
election_campaign.cpp:250:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
  250 | main()
      | ^~~~
election_campaign.cpp: In function 'int main()':
election_campaign.cpp:256:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  256 |         freopen(__Azul__".inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
election_campaign.cpp:257:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  257 |         freopen(__Azul__".out", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... |