#include <stdio.h>
#include <algorithm>
#include <vector>
#include <tuple>
using namespace std;
int N; vector<pair<int, int> > A[252525], B[252525];
int S[252525]; bool C[252525];
vector<pair<int, long long> > U[252525], L;
long long R[252525];
struct min2 {
min2() { val[0] = val[1] = { 4e18, -1 }; }
pair<long long, int> val[2];
void push(pair<long long, int> p) {
if (val[0] > p) tie(val[1], val[0]) = tie(val[0], p);
else if (val[1] > p) val[1] = p;
}
long long pop(int y)
{
return val[val[0].second == y].first;
}
}M[252525];
void dfs(vector<pair<int, int> >* T, int x, int l, long long d)
{
L.emplace_back(x,d);
S[x] = 1;
for (auto [y, c] : T[x]) if (y != l && !C[y]){
dfs(T, y, x, d + c);
S[x] += S[y];
}
}
int center(vector<pair<int, int> >* T, int x)
{
L.clear();
dfs(T, x, -1, 0);
int s = L.size();
for (auto [y, w] : L) {
bool g = 1;
for (auto [z, u] : T[y]) if (!C[z]){
if (S[y] > S[z] && S[z] * 2 > s) { g = 0; break; }
if (S[y] < S[z] && S[y] * 2 < s) { g = 0; break; }
}
if (g) {
L.clear();
dfs(T, y, -1, 0);
return y;
}
}
return -1;
}
void decomp1(int x)
{
int c = center(A, x);
for (auto [y, w] : L) U[y].emplace_back(c, w);
C[c] = 1;
for (auto [y, w] : A[c]) if (!C[y]) decomp1(y);
C[c] = 0;
}
void decomp2(int x)
{
int c = center(B, x);
for (auto [y, w] : L) for (auto [z, u] : U[y]) M[z] = min2();
for (auto [y, w] : L) for (auto [z, u] : U[y]) M[z].push({ w + u, y });
for (auto [y, w] : L) for (auto [z, u] : U[y]) R[y] = min(R[y], w + u + M[z].pop(y));
C[c] = 1;
for (auto [y, w] : B[c]) if (!C[y]) decomp2(y);
C[c] = 0;
}
int main()
{
scanf ("%d", &N);
for (int i = 1; i < N; i++) {
int x, y, w; scanf ("%d %d %d", &x, &y, &w);
A[x].emplace_back(y, w);
A[y].emplace_back(x, w);
}
for (int i = 1; i < N; i++) {
int x, y, w; scanf ("%d %d %d", &x, &y, &w);
B[x].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