#include<bits/stdc++.h>
using namespace std;
const int maxn=1e5+10;
bool back=false;
int n,q,ans;
vector<pair<int,int> >e[maxn];
queue<pair<int,int> > p;
void dfs(int d){
while(!p.empty()){
pair<int,int> now=p.front();
p.pop();
bool end=true;
for(auto i:e[now.first]){
if(!(e[now.first].second%d)){
if(back){
back=false;
ans-=now.second*(now.second-1);
}
end=false;
p.push(make_pair(e[now.first].first,now.second+1));
}
}
if(end){
back=true;
ans+=now.second*(now.second-1);
}
}
}
int main(){
ios::sync_with_stdio(0);
cin.tie(0),cout.tie(0);
cin >> n >> q;
for(int i=1;i<=n;i++){
int u,v,w;
cin >> u >> v >> w;
e[u].push_back(make_pair(v,w));
e[v].push_back(make_pair(u,w));
}
int d;
ans=0;
while(q--){
cin >> d;
p.push(make_pair(1,0));
dfs(d);
cout<<ans<<endl;
back=false;
ans=0;
}
return 0;
}