#include<bits/stdc++.h>
using namespace std;
#define rep(i,s,t) for (int i=s;i<=t;i++)
#define pi acos(-1)
typedef long long LL;
typedef pair<int,int> PII;
typedef pair<double, double> PDD;
typedef pair<PII, PII> PPP;
typedef pair<PII, int> PPI;
#define repp(i,s,t) for (int i=s;i>=t;i--)
template<class T> T sqr(T x) {return x*x;}
#define debug(x) cerr<<#x"="<<(x)<<endl;
#define pb(x) push_back(x);
#define ori(x) x-'a'
const int maxn = 3001;
vector<PII> g[maxn];
int w[maxn];
LL path_list[maxn];
vector<LL> gao;
int cnt;
int n, m, x, y, z;
LL dfs(int k, int last)
{
	LL ret = 0;
	for (int i = 0 ; i < g[k].size(); i++)
	if (g[k][i].first != last)
	{
		LL tmp = dfs(g[k][i].first, k) + g[k][i].second;
//		cout << k << " " << last << " " << g[k][i].first << " " << tmp << endl;
		LL minlen = min(tmp, ret);
		ret = max(tmp, ret);
		if (minlen) path_list[++cnt] = minlen;
	}
	return ret;
}
int main()
{
	int testdata;
	scanf("%d",&testdata);
	while (testdata--)
	{
		scanf("%d%d",&n,&m);
		rep(i,1,n) g[i].clear();
		rep(i,1,n-1)
		{
			scanf("%d%d%d",&x,&y,&z);
			g[x].push_back(PII(y, z));
			g[y].push_back(PII(x, z));
		}
		rep(i,1,n) scanf("%d", &w[i]);
		rep(i,1,n)
		{
			cnt = 0;
			LL res = dfs(i, -1);
			path_list[++cnt] = res;
			rep(j,1,cnt) gao.push_back(path_list[j] * w[i]);
		}
		int mm = min(m, (int)gao.size());
		nth_element(gao.begin(), gao.end() - m, gao.end());
		LL ret = 0;
		for (int i = gao.size() - mm; i < gao.size(); i++) ret += gao[i];
		printf("%lld\n", ret);
		gao.clear();
	}
}

