> File Name: 5764.cpp
> Author: jiangyuzhu
> Mail: 834138558@qq.com
> Created Time: 2016/9/30 12:57:02
************************************************************************/
#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#include<vector>
#include<set>
#include<map>
#include<algorithm>
using namespace std;
const int maxn = 1e5 + 5, maxm= 20, oo = 0x3f3f3f3f;
struct EDGE{
int next, to;
}edge[maxn * 2 + 5];
int tot;
int head[maxn];
int used[maxn];
int a[maxn];
int ori[maxn];
int n;
vector<int>ve[maxn];
void init()
{
memset(ori, 0, sizeof(ori));
memset(used, 0, sizeof(used));
memset(head, -1, sizeof(head));
tot = 0;
for(int i = 1; i <= n; ++i) ve[i].clear();
}
void addedge(int from, int to)
{
edge[tot].to = to;
edge[tot].next = head[from];
head[from] = tot++;
}
int root;
void getroot(int u, int pa)
{
int cnt = 0;
bool flag = false;
for(int i = head[u]; i != -1; i = edge[i].next){
int v = edge[i].to;
if(v == pa) continue;
if(a[v] == n){
getroot(v, u);
flag = true;
}
}
if(!flag) root = min(root, u);
}
bool dfs(int u, int pa)
{
int maxx = 0;
int cnt = 0;
for(int i = head[u]; i != -1; i = edge[i].next){
int v = edge[i].to;
if(v == pa) continue;
if(!dfs(v, u)) return false;
maxx = max(maxx, a[v]);
if(a[v] == a[u]) cnt++;
}
if(a[u] < maxx) return false;
else if(a[u] == maxx && cnt != 1) return false;
else if(a[u] > maxx){
ori[u] = a[u];
if(used[a[u]]) return false;
used[a[u]] = u;
}
return true;
}
void solve()
{
scanf("%d", &n);
int u, v;
root = -1;
for(int i = 1; i <= n; ++i){
scanf("%d", &a[i]);
ve[a[i]].push_back(i);
if(a[i] == n) root = i;
}
for(int i = 1; i < n; ++i){
scanf("%d%d", &u, &v);
addedge(u, v);
addedge(v, u);
}
if(root == -1){
puts("Impossible");
return;
}
getroot(root, root);
if(!dfs(root, root)){
puts("Impossible");
return;
}
priority_queue<int>q;
for(int i = n; i >= 1; --i){
if(used[i]){
int sz = ve[i].size();
for(int j = 0; j < sz; ++j){
if(ve[i][j] == used[i]) continue;
q.push(ve[i][j]);
}
}else{
if(q.empty()){
puts("Impossible");
return;
}
int t = q.top();q.pop();
ori[t] = i;
}
}
for(int i = 1; i <= n; ++i) printf("%d%c", ori[i], i == n?'\n':' ');
return;
}
int main (void)
{
int T;scanf("%d", &T);
for(int tt = 1; tt <= T; tt++){
printf("Case #%d: ", tt);
init();
solve();
}
return 0;
}