#include<iostream>#include<cstring>#include<map>#include<vector>usingnamespace std;constint N =1e5+10;vector<int> G[N], G2[N];int fa[N];bool st[N];int c[N], a[N];
map<int,int> dc;voidinit(int n){for(int i =1; i <= n; i ++)fa[i]= i;}intfind(int x){if(fa[x]!= x) fa[x]=find(fa[x]);return fa[x];}voidmerge(int a,int b){a =find(a), b =find(b);if(a != b) fa[a]= b;}// 求點的深度voiddfs(int u){for(auto v : G2[u]){a[v]= a[u]+1;dfs(v);}}intmain(){ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);int T;cin >> T;while(T --){int n;cin >> n;// 并查集初始化init(n);// 多組數據for(int i =1; i <= n; i ++) G[i].clear(), G2[i].clear();memset(st,0,sizeof st);dc.clear();// 樹,無向圖,注意是n - 1條邊!!!for(int i =0; i < n -1; i ++){int u, v;cin >> u >> v;G[u].push_back(v);G[v].push_back(u);}// map 點權-點編號for(int i =1; i <= n; i ++){cin >> c[i];dc[c[i]]= i;}st[dc.begin()-> second]=true;// 點權值最小的點為根,第一個訪問for(auto it =++ dc.begin(); it != dc.end(); it ++){auto u = it -> second;for(auto v : G[u])// 原圖中相連的點{if(!st[v])continue;// 已經枚舉過的連通塊int cc =find(v);merge(v, u);// 將已經枚舉過的連通塊的根接在u下G2[u].push_back(cc);}st[u]=true;// 已經訪問過u}int x =(-- dc.end())-> second;a[x]=1;dfs(x);for(int i =1; i <= n; i ++) cout << a[i]<< endl;}return0;}