#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #define REP(i,a,b) for(int i=(a);i<=(b);i++) #define PER(i,a,b) for(int i=(a);i>=(b);i--) #define RVC(i,S) for(int i=0;i<(S).size();i++) #define RAL(i,u) for(int i=fr[u];i!=-1;i=e[i].next) using namespace std; typedef long long LL; typedef pair pii; template inline void read(T& num) { bool start=false,neg=false; char c; num=0; while((c=getchar())!=EOF) { if(c=='-') start=neg=true; else if(c>='0' && c<='9') { start=true; num=num*10+c-'0'; } else if(start) break; } if(neg) num=-num; } /*============ Header Template ============*/ struct edge { int next,to; }; const int maxn=1100; const int mo=(int)(1e9)+7; const int iv=mo+1>>1; int f[maxn][maxn]; int fr[maxn]; edge e[maxn<<1]; int a[maxn]; int res[maxn]; int n,m,tote=0; inline void addone(int u,int v) { ++tote; e[tote].next=fr[u];fr[u]=tote;e[tote].to=v; } inline void addedge(int u,int v) { addone(u,v);addone(v,u); } void print(int x) { REP(k,0,m-1) printf("%d ",f[x][k]);putchar('\n'); } void fwt(int*a,int n){ int i,j,k,x; for(k=2;k<=n;k<<=1){ for(i=0;i>1);j++){ x=a[j]+a[j+(k>>1)];if(x>=mo) x-=mo; a[j]=a[j]-a[j+(k>>1)];if(a[j]<0) a[j]+=mo; a[j+(k>>1)]=x; } } } } void twf(int*a,int n){ int i,j,k,x; for(k=n;k>=2;k>>=1){ for(i=0;i>1);j++){ x=a[j]+a[j+(k>>1)]; a[j+(k>>1)]=(int)(1LL*(a[j+(k>>1)]-a[j]+mo)*iv%mo); a[j]=(int)(1LL*x*iv%mo); } } } } int ta[maxn]; void dfs(int x,int g) { f[x][a[x]]++; RAL(i,x) if(e[i].to!=g) { dfs(e[i].to,x); REP(k,0,m-1) ta[k]=f[x][k]; fwt(ta,m); REP(k,0,m-1) ta[k]=1LL*ta[k]*f[e[i].to][k]%mo; twf(ta,m); REP(k,0,m-1) (f[x][k]+=ta[k])%=mo; } REP(k,0,m-1) { res[k]+=f[x][k]; if(res[k]>=mo) res[k]-=mo; } fwt(f[x],m); } void solve() { read(n);read(m); REP(i,1,n) read(a[i]); memset(fr,-1,sizeof(fr));tote=0; memset(f,0,sizeof(f)); REP(i,1,n-1) { int u,v; read(u);read(v); addedge(u,v); } memset(res,0,sizeof(res)); dfs(1,0); REP(i,0,m-1) { if(i!=0) putchar(' '); printf("%d",res[i]); } putchar('\n'); } int main() { int T; read(T); while(T--) solve(); return 0; }