#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<iomanip>
#include<vector>
#include<string>
#include<cstring>
using namespace std;
#define N 50000
int a[N], vis[N], dist[N];
int n, m, t;
int x[N], y[N], z[N];
struct node
{
int next;
int to;
int w;
};
node s[3 * N]; int head[N];
void addeadge(int x, int y, int z)
{
s[t].to = y;
s[t].w = z;
s[t].next = head[x];
head[x] = t; t++;
s[t].to = x;
s[t].w = z;
s[t].next = head[y];
head[y] = t;
t++;
}
int find(int x)
{
return a[x] == x ? x : a[x] = find(a[x]);
}
void LCA(int i)
{
vis[i] = 1;
a[i] = i;
int k;
//cout << i << endl;
/*for (int j = 0; j<m; j++)
{
//cout << i << " " << x[j] << " " << y[j] << vis[y[j]] <<" "<< vis[x[j]] << endl;
if (x[j] == i && vis[y[j]] == 1)
{
z[j] = find(y[j]);
}
if (y[j] == i && vis[x[j]] == 1)
{
z[j] = find(x[j]);
}
}*/
for (int j = head[i]; j != -1; j = s[j].next)
{
k = s[j].to;
if (vis[k] == 1)continue;
//cout << i << " " << k << endl;
dist[k] = dist[i] + s[j].w;
LCA(k);
a[k] = i;
}
for (int j = 0; j<m; j++)
{
//cout << i << " " << x[j] << " " << y[j] << vis[y[j]] <<" "<< vis[x[j]] << endl;
if (x[j] == i && vis[y[j]] == 1)
{
z[j] = find(y[j]);
}
if (y[j] == i && vis[x[j]] == 1)
{
z[j] = find(x[j]);
}
}
}
int main()
{
int xx, yy, k, zz;
int T;
cin >> T;
while (T--)
{
cin >> n >> m;
memset(head, -1, sizeof(head));
memset(vis, 0, sizeof(vis));
memset(dist, 0, sizeof(0));
t = 0;
for (int i = 1; i<n; i++)
{
cin >> xx >> yy >> zz;
addeadge(xx, yy, zz);
}
for (int j = 0; j<m; j++)
{
cin >> x[j] >> y[j];
}
LCA(1);
for (int i = 0; i < m; i++)
{
//cout << x[i] << y[i] << z[i] << endl;
cout << dist[x[i]] + dist[y[i]] - 2 * dist[z[i]] << endl;
}
}
return 0;
#include<iostream>
#include<iomanip>
#include<vector>
#include<string>
#include<cstring>
using namespace std;
#define N 50000
int a[N], vis[N], dist[N];
int n, m, t;
int x[N], y[N], z[N];
struct node
{
int next;
int to;
int w;
};
node s[3 * N]; int head[N];
void addeadge(int x, int y, int z)
{
s[t].to = y;
s[t].w = z;
s[t].next = head[x];
head[x] = t; t++;
s[t].to = x;
s[t].w = z;
s[t].next = head[y];
head[y] = t;
t++;
}
int find(int x)
{
return a[x] == x ? x : a[x] = find(a[x]);
}
void LCA(int i)
{
vis[i] = 1;
a[i] = i;
int k;
//cout << i << endl;
/*for (int j = 0; j<m; j++)
{
//cout << i << " " << x[j] << " " << y[j] << vis[y[j]] <<" "<< vis[x[j]] << endl;
if (x[j] == i && vis[y[j]] == 1)
{
z[j] = find(y[j]);
}
if (y[j] == i && vis[x[j]] == 1)
{
z[j] = find(x[j]);
}
}*/
for (int j = head[i]; j != -1; j = s[j].next)
{
k = s[j].to;
if (vis[k] == 1)continue;
//cout << i << " " << k << endl;
dist[k] = dist[i] + s[j].w;
LCA(k);
a[k] = i;
}
for (int j = 0; j<m; j++)
{
//cout << i << " " << x[j] << " " << y[j] << vis[y[j]] <<" "<< vis[x[j]] << endl;
if (x[j] == i && vis[y[j]] == 1)
{
z[j] = find(y[j]);
}
if (y[j] == i && vis[x[j]] == 1)
{
z[j] = find(x[j]);
}
}
}
int main()
{
int xx, yy, k, zz;
int T;
cin >> T;
while (T--)
{
cin >> n >> m;
memset(head, -1, sizeof(head));
memset(vis, 0, sizeof(vis));
memset(dist, 0, sizeof(0));
t = 0;
for (int i = 1; i<n; i++)
{
cin >> xx >> yy >> zz;
addeadge(xx, yy, zz);
}
for (int j = 0; j<m; j++)
{
cin >> x[j] >> y[j];
}
LCA(1);
for (int i = 0; i < m; i++)
{
//cout << x[i] << y[i] << z[i] << endl;
cout << dist[x[i]] + dist[y[i]] - 2 * dist[z[i]] << endl;
}
}
return 0;
}
/*自己写这个的时候 犯了比较严重的错误 自己的竟然把LCA和输入数据写反了 害我找了很长时间的错误 下次 注意 还有那个判断最近祖先的循环可以放到前面或者后面 都没有问题*/