1 const int maxn = 4e4 + 5; 2 int T, n, m; 3 int f[maxn], vis[maxn], dis[maxn], ans[maxn]; 4 vector<P> vc[maxn]; 5 vector<int> query[maxn], id[maxn]; 6 7 inline int getf(int v) { return v == f[v] ? v : f[v] = getf(f[v]); } 8 9 inline void Tarjan(int cur) { 10 vis[cur] = 1; 11 12 for (auto son : vc[cur]) { 13 if (vis[son.first]) continue; 14 15 dis[son.first] = dis[cur] + son.second; 16 Tarjan(son.first); 17 f[son.first] = cur; 18 } 19 20 vis[cur] = 2; 21 22 for (int i = 0; i < query[cur].size(); i++) { 23 int v = query[cur][i], t = id[cur][i]; 24 25 if (vis[v] == 2) { 26 int lca = getf(v); 27 ans[t] = dis[cur] + dis[v] - 2 * dis[lca]; 28 } 29 } 30 } 31 32 int main() { 33 for (read(T); T; T--) { 34 read(n), read(m); 35 36 rep(i, 1, n) { 37 vc[i].clear(), query[i].clear(), id[i].clear(); 38 f[i] = i, vis[i] = 0; 39 } 40 41 rep(i, 1, n - 1) { 42 int u, v, cost; 43 read(u), read(v), read(cost); 44 vc[u].push_back({v, cost}); 45 vc[v].push_back({u, cost}); 46 } 47 48 auto Add_Query = [](int u, int v, int i) { 49 query[u].push_back(v); 50 id[u].push_back(i); 51 }; 52 53 rep(i, 1, m) { 54 int u, v; 55 read(u), read(v); 56 Add_Query(u, v, i); 57 if(u != v) Add_Query(v, u, i); 58 } 59 60 Tarjan(1); 61 62 rep(i, 1, m) 63 writeln(ans[i]); 64 } 65 return 0; 66 }