#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
using namespace std;
#define MAXN 500010
struct _node
{
    int u,v,w;
    bool operator < (const _node &b) const
    {
        return w<b.w;
    }
}no[MAXN];
struct _query
{
    int t,id;
    bool operator < (const _query &b) const
    {
        return t<b.t;
    }
}query[MAXN];
int fa[MAXN],ran[MAXN],ans[MAXN],sum;
int findfa(int n)
{
    if(fa[n]!=n)
        fa[n]=findfa(fa[n]);
    return fa[n];
}
inline void mul(int a,int b)
{
    a=findfa(a),b=findfa(b);
    int af=ran[a],bf=ran[b];
    if(a==b)    return ;
    if(af<bf)   swap(a,b);
    fa[b]=a,ran[a]+=ran[b];
    sum-=af*(af-1),sum-=bf*(bf-1);
    sum+=ran[a]*(ran[a]-1);
}
int main()
{
    //freopen(\\\"/home/moor/Code/input\\\",\\\"r\\\",stdin);
    int n,m,q;
    while(scanf(\\\"%d%d\\\",&n,&m)==2)
    {
        for(int i=0;i<m;++i)
            scanf(\\\"%d%d%d\\\",&no[i].u,&no[i].v,&no[i].w),++no[i].u,++no[i].v;
        for(int i=1;i<=n;++i)   fa[i]=i,ran[i]=1;
        sort(no,no+m);
        scanf(\\\"%d\\\",&q);
        for(int i=0;i<q;++i)    scanf(\\\"%d\\\",&query[i].t),query[i].id=i;
        sort(query,query+q);
        sum=0;
        int l=0;
        for(int i=0;i<q;++i)
        {
            while(l<m&&no[l].w<query[i].t)
                mul(no[l].u,no[l].v),++l;
            ans[query[i].id]=sum;
        }
        while(l<m)  mul(no[l].u,no[l].v),++l;
        for(int i=0;i<q;++i)    printf(\\\"%d\\\\n\\\",sum-ans[i]);
    }
    return 0;
}