#include<iostream>
#include<sstream>
#include<string>
#include<vector>
#include<list>
#include<set>
#include<map>
#include<stack>
#include<queue>
#include<algorithm>
#include<cmath>
#pragma warning(disable:4996)
using namespace std;
class Edge
{
public:
int next;
int weight;
Edge(int a = 0, int b = 0)
{
next = a, weight = b;
}
};
class Graph
{
public:
vector<vector<Edge>>adjList;
Graph(const int &node_num)
{
adjList.resize(40010);
for (int i = 0; i != node_num - 1; i++)
{
int a, b, weight; cin >> a >> b >> weight;
adjList[a].push_back({ b,weight });
adjList[b].push_back({ a,weight });
}
}
int BFS(const int &start,const int &end)
{
queue<int>Q_path, Q_node;
vector<bool>visited(40010);
visited[start] = true;
for (int i = 0; i != adjList[start].size(); i++)
{
Q_node.push(adjList[start][i].next);
Q_path.push(adjList[start][i].weight);
}
while (true)
{
auto curnode = Q_node.front(); Q_node.pop();
auto curpath = Q_path.front(); Q_path.pop();
if (curnode == end)
{
return curpath;
}
for (int i = 0; i != adjList[curnode].size(); i++)
{
if (!visited[adjList[curnode][i].next])
{
Q_node.push(adjList[curnode][i].next);
Q_path.push(adjList[curnode][i].weight + curpath);
visited[adjList[curnode][i].next] = true;
}
}
}
return 0;
}
};
int main()
{
//freopen("input.txt", "r", stdin);
//freopen("output.txt", "w", stdout);
int T; cin >> T;
while (T--)
{
int n, m; cin >> n >> m;
Graph graph(n);
while (m--)
{
int a, b; cin >> a >> b;
cout << graph.BFS(a,b) << endl;
}
}
return 0;
}
#include<iostream>
#include<sstream>
#include<string>
#include<vector>
#include<list>
#include<set>
#include<map>
#include<stack>
#include<queue>
#include<algorithm>
#include<cmath>
#pragma warning(disable:4996)
using namespace std;
int n, cnt, icnt;
int head[80000 + 100], l[80000 + 100], ll[80000 + 100], mark[80705], depth[80000 + 100], dist[80000 + 100];
int dp[80000 + 100][30];
class Node
{
public:
int to, next, weight;
};
Node e[80000 + 100];
void addEdge(const int &a,const int &b,const int &w)
{
e[cnt].to = b;
e[cnt].next = head[a];
e[cnt].weight = w;
head[a] = cnt++;
}
void dfs(int u, int d)
{
mark[u] = 1;
l[++icnt] = u;
ll[u] = icnt;
depth[icnt] = d;
for (int i = head[u]; i + 1; i = e[i].next)
{
int to = e[i].to;
if (mark[to])
continue;
int w = e[i].weight;
dist[to] = dist[u] + w;
dfs(to, d + 1);
l[++icnt] = u;
depth[icnt] = d;
}
}
void RMQ()
{
int i, j;
for (i = 1; i <= icnt; i++)
dp[i][0] = i;
for (i = 1; (1 << i) <= icnt; i++)
{
for (j = 1; j + (1 << i) <= icnt; j++)
{
int a = dp[j][i - 1], b = dp[j + (1 << i - 1)][i - 1];
dp[j][i] = depth[a]<depth[b] ? a : b;
}
}
}
int St(int a, int b)
{
int k = (int)((log((double)(b - a + 1))) / log(2.0));
int x = dp[a][k], y = dp[b - (1 << k) + 1][k];
int t = depth[x]<depth[y] ? x : y;
return t;
}
int Lca(int a, int b)
{
int x = ll[a], y = ll[b];
if (x>y) swap(x, y);
int st = St(x, y);
return l[st];
}
int main()
{
int T, m, a, b, c, i;
scanf("%d", &T);
while (T--)
{
memset(head, -1, sizeof(head));
memset(mark, 0, sizeof(mark));
cnt = 0;
scanf("%d%d", &n, &m);
for (i = 1; i <= n - 1; i++)
{
scanf("%d%d%d", &a, &b, &c);
addEdge(a, b, c);
addEdge(b, a, c);
}
icnt = 0;
dfs(1, 1);
RMQ();
while (m--)
{
scanf("%d%d", &a, &b);
int lca = Lca(a, b);
printf("%d\n", dist[b] + dist[a] - 2 * dist[lca]);
}
}
return 0;
}