#include <bits/stdc++.h>
#include "factories.h"
#define fi first
#define se second
#define pb push_back
#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 FORLL(i, a, b) for (ll i = a, _b = b; i <= _b; ++i)
#define FORDLL(i, a, b) for (ll i = a, _b = b; i >= _b; --i)
#define all(x) x.begin(), x.end()
#define uni(x) sort(all(x)), x.erase(unique(all(x)), x.end())
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
#define dbg(...) debug(#__VA_ARGS__, __VA_ARGS__)
template<typename T>
void __print_one(const char *&s, const T &x)
{
while (*s == ' ') ++s;
const char *p = s;
int bal = 0;
while (*s)
{
if (*s == '(') ++bal;
else if (*s == ')') --bal;
else if (*s == ',' && bal == 0) break;
++s;
}
cerr.write(p, s - p) << " = " << x;
if (*s == ',')
{
++s;
cerr << " , ";
}
}
template<typename... Args>
void debug(const char *s, Args... args)
{
cerr << "[ ";
int dummy[] = { 0 , ( __print_one(s, args) , 0 )... };
(void)dummy;
cerr << " ]\n\n";
}
template<class X>
bool maximize(X &a, X b)
{
if (a < b)
{
a = b;
return true;
}
return false;
}
template<class X>
bool minimize(X &a, X b)
{
if (a > b)
{
a = b;
return true;
}
return false;
}
// --------------------------------------------------------------------------------------------
const int maxn = 5e5 + 3;
int n, q, h[maxn], tin[maxn], tour[maxn << 1][20], lg[maxn << 1], tdfs;
ll D[maxn], dist[maxn];
vector<pii> g[maxn];
// --------------------------------------------------------------------------------------------
void dfs_pre(int u, int p)
{
tin[u] = ++tdfs;
tour[tdfs][0] = u;
for (const auto &e : g[u])
{
if (e.fi == p) continue;
D[e.fi] = D[u] + e.se;
h[e.fi] = h[u] + 1;
dfs_pre(e.fi, u);
tour[++tdfs][0] = u;
}
}
#define MIN_H(x, y) (h[x] < h[y] ? x : y)
int lca(int u, int v)
{
u = tin[u]; v = tin[v];
if (u > v) swap(u, v);
int k = lg[v - u + 1];
return MIN_H(tour[u][k], tour[v - (1 << k) + 1][k]);
}
ll get_dist(int u, int v)
{
return D[u] + D[v] - 2 * D[lca(u, v)];
}
void Init(int N, int A[], int B[], int D[])
{
n = N;
FOR(i, 0, n - 1)
{
g[i].clear();
}
FOR(i, 0, n - 2)
{
int u = A[i], v = B[i], w = D[i];
if (u > v) swap(u, v);
g[u].push_back({v, w});
g[v].push_back({u, w});
}
tdfs = 0;
dfs_pre(0, -1);
FOR(i, 2, tdfs)
lg[i] = lg[i >> 1] + 1;
FOR(j, 1, 19)
FOR(i, 1, tdfs - (1 << j) + 1)
tour[i][j] = MIN_H(tour[i][j - 1], tour[i + (1 << j - 1)][j - 1]);
}
ll Query(int S, int X[], int T, int Y[])
{
int nA = S, nB = T;
ll res = 1e18;
if (1ll * nA * nB <= ll(1e5))
{
FOR(i, 0, nA - 1)
FOR(j, 0, nB - 1)
{
res = min(res, get_dist(X[i], Y[j]));
}
}
else
{
priority_queue<pll, vector<pll>, greater<pll>> Q;
FOR(i, 0, n - 1)
dist[i] = ll(1e18);
FOR(i, 0, nB - 1)
{
Q.push({0ll, Y[i]});
dist[Y[i]] = 0;
}
while (Q.size())
{
ll w; int u; tie(w, u) = Q.top(); Q.pop();
if (w > dist[u]) continue;
for (const auto &e : g[u])
{
if (dist[e.fi] > w + e.se)
{
dist[e.fi] = w + e.se;
Q.push({dist[e.fi], e.fi});
}
}
}
FOR(i, 0, nA - 1)
{
res = min(res, dist[X[i]]);
}
}
return res;
}
signed main()
{
ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
#define TASK "TEST"
if (fopen(TASK".INP", "r"))
{
freopen(TASK".INP", "r", stdin);
freopen(TASK".OUT", "w", stdout);
}
solve();
return 0;
}