#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp> // Common file
#include <ext/pb_ds/tree_policy.hpp> // Including tree_order_statistics_node_update
#define int long long
#define mod 1000000007
#define maxN 100005
#define pb push_back
#define mp make_pair
#define pii pair<int,int>
#define pip pair<int,pii>
#define vi vector<int>
#define vpi vector<pii >
#define endl "\n"
#define fastIO ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0)
#define FOR(a,b,c) for(int(a) = b;a<=c;a++)
#define repr(a,b,c) for(int(a) = b;a>=c;a--)
#define rep(i,n) for(int(i) = 0;i<n;i++)
#define fir first
#define sec second
#define beg begin()
#define e end()
#define len length()
using namespace std;
using namespace __gnu_pbds;
typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update> ordered_set;
typedef tree<int,null_type,less_equal<int>,rb_tree_tag,tree_order_statistics_node_update> ordered_multiset;
bool comp(pii a,pii b){
return a.sec < b.sec;
}
int val[maxN];
int q;
vpi g[maxN];
pii mark[maxN];
int tim = 0;
void dfs(int x,int par){
rep(i,g[x].size()){
int y = g[x][i].fir;
if(y == par)continue;
tim++;
int w = g[x][i].sec;
int ind = upper_bound(val,val+q,w) - val;
mark[ind] = mp(y,tim);
dfs(y,x);
}
}
int32_t main()
{
fastIO;
int t;
cin>>t;
while(t--){
int n;
tim = 0;
cin>>n>>q;
int x,y,w;
rep(i,n-1){
cin>>x>>y>>w;
g[x].pb(mp(y,w));
g[y].pb(mp(x,w));
}
FOR(i,1,n){
sort(g[i].beg,g[i].e,comp);
}
rep(i,q){
cin>>val[i];
mark[i]=mp(1,0);
}
sort(val,val+q);
dfs(1,1);
int ans = 0;
int last = mark[0].fir;
tim = mark[0].sec;
rep(i,q){
//cout<<mark[i]<<' ';
if(mark[i].sec > tim){
last = mark[i].fir;
tim = mark[i].sec;
}
ans+=last;
}
cout<<ans<<endl;
FOR(i,1,n)g[i].clear();
}
return 0;
}
I2luY2x1ZGU8Yml0cy9zdGRjKysuaD4KI2luY2x1ZGUg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AgaW50IGxhc3QgPSBtYXJrWzBdLmZpcjsKICAgICB0aW0gPSBtYXJrWzBdLnNlYzsKICAgICByZXAoaSxxKXsKICAgICAgIC8vY291dDw8bWFya1tpXTw8JyAnOwogICAgICAgaWYobWFya1tpXS5zZWMgPiB0aW0pewogICAgICAgICBsYXN0ID0gbWFya1tpXS5maXI7CiAgICAgICAgIHRpbSA9IG1hcmtbaV0uc2VjOwogICAgICAgfQogICAgICAgYW5zKz1sYXN0OwogICAgIH0KICAgICBjb3V0PDxhbnM8PGVuZGw7CiAgICAgRk9SKGksMSxuKWdbaV0uY2xlYXIoKTsKICAgfQoKICAgcmV0dXJuIDA7Cn0K