#include <bits/stdc++.h>
using namespace std;
#define int int64_t
#define endl '\n'
#define vi vector<int>
#define vvi vector<vi>
#define F0(n, i) for(int i = 0; i < n; i++)
#define F1(n, i) for(int i = 1; i <= n; i++)
#define each(a) for(auto& e: a)
#define pb push_back
#define arr(a) { each(a) cerr << e << ' '; cerr << endl; }
#define mtx(m) each(m) arr(e);
void solve() {
int n;
cin >> n;
vi a(n), prt(n), vis(n), tin(n), tout(n), node(n), ans(n);
F0(n, i) cin >> a[i], node[a[i]] = i;
vvi G(n);
F0(n - 1, _) {
int u, v;
cin >> u >> v;
G[u].pb(v);
G[v].pb(u);
}
function<void(int, int)> dfs = [&](int i, int p) {
prt[i] = p;
each(G[i]) {
if(e != p) dfs(e, i);
}
};
mtx(G);
dfs(0, -1);
int t = 0;
function<void(int)> dfs_2 = [&](int i) {
vis[i] = 1;
tin[i] = t++;
each(G[i]) {
if(!vis[e]) dfs_2(e);
}
tout[i] = t++;
};
dfs_2(0);
int curr = node[0], mex = 0;
while(curr != -1) {
while(mex < n) {
int u = node[mex];
if(tin[u] < tin[curr] || tout[u] > tout[curr]) break ;
mex++;
}
ans[curr] = mex;
curr = prt[curr];
}
each(ans) cout << e << ' ';
}
int32_t main() {
int t = 1;
cin >> t;
while(t--) {
solve();
cout << endl;
cerr << endl;
}
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CnVzaW5nIG5hbWVzcGFjZSBzdGQ7CgojZGVmaW5lIGludCBpbnQ2NF90CiNkZWZpbmUgZW5kbCAnXG4nCiNkZWZpbmUgdmkgdmVjdG9yPGludD4KI2RlZmluZSB2dmkgdmVjdG9yPHZpPgojZGVmaW5lIEYwKG4sIGkpIGZvcihpbnQgaSA9IDA7IGkgPCBuOyBpKyspCiNkZWZpbmUgRjEobiwgaSkgZm9yKGludCBpID0gMTsgaSA8PSBuOyBpKyspCiNkZWZpbmUgZWFjaChhKSBmb3IoYXV0byYgZTogYSkKI2RlZmluZSBwYiBwdXNoX2JhY2sKI2RlZm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