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5 1
2 2
2 3
 83 8
54 3 52 12 61 36 65 62 69 49 47 77 31 15 21 14 73 29 6 26 37 17 81 75 43 30 58 76 16 8 11 5 27 35 7 66 50 67 2 39 45 28 60 71 38 82 53 1 42 13 44 72 22 4 9 25 19 57 10 70 18 68 32 34 20 80 23 79 24 63 64 51 59 41 74 48 40 33 46 83 55 56 78
48 80
1 8
71 54
15 59
72 46
36 9
64 29
55 58
 1 1
1
1 1
 5 20
3 4 1 5 2
2 4
3 1
2 2
4 5
4 5
5 2
1 1
2 4
3 3
4 2
3 1
1 1
5 2
1 3
3 1
4 2
1 3
3 3
4 2
4 2
 6 20
4 6 1 3 2 5
2 2
6 5
3 4
3 4
5 6
3 3
5 5
6 6
4 3
2 2
2 2
5 2
3 4
1 4
5 2
4 3
2 5
1 4
3 1
4 3
 2 10
1 2
1 1
2 2
2 2
1 1
1 1
2 2
2 2
1 1
2 2
1 1
 5 3
5 2 3 4 1
2 4
1 4
3 4
 4 1
4 3 1 2
3 4
 3 1
1 3 2
1 2
 2 10
2 1
2 1
1 2
1 1
2 1
1 1
2 1
1 1
1 1
2 1
2 1
 3 1
2 3 1
1 1
 8 1
3 4 1 2 7 8 5 6
3 4
 3 10
1 2 3
2 2
1 1
2 2
3 3
1 1
3 3
3 3
3 3
2 2
1 1

3 2 1 
 6 2 5 7 3 1 4 
 4 2 3 1 
 9 8 6 7 5 3 4 2 1 
 7 6 5 4 3 2 1 
 3 2 1 
 8 4 7 2 6 5 3 1 
 2 3 1 
 4 12 7 1 16 19 3 10 14 8 13 2 11 9 20 5 18 17 6 15 
 6 8 9 4 5 1 7 2 3 
 5 4 3 2 1 
 3 4 1 2 
 5 3 2 4 1 6 
 62 3 52 12 61 36 65 54 69 49 47 77 31 15 21 14 73 29 6 26 37 17 81 75 43 30 58 76 34 8 11 5 27 35 7 66 50 67 2 39 45 28 60 71 38 82 53 83 42 13 44 72 22 64 57 25 19 9 10 70 18 68 32 16 20 80 23 79 24 63 4 51 59 41 74 48 40 33 46 1 55 56 78 
 1 
 3 5 1 4 2 
 4 6 3 1 5 2 
 1 2 
 5 4 3 2 1 
 4 3 2 1 
 3 1 2 
 2 1 
 2 3 1 
 3 4 2 1 7 8 5 6 
 1 2 3 

                    /**************************************************************************************
                    *                                        Poet :                                       *
                    |                                       Arnab048                                      |
                    *                                   (codingjedi048)                                   *
                    |                               codingjedi048@gmail.com                               |
                    **************************************************************************************//*
                                            I have a clumsy apprentice called Einstein.                                          
*/#include<bits/stdc++.h>
using namespace std;
//constants : 
const int   N                               = (int) 1e6+5;
const int   M                               = (int) 1e9+5;
const int   mod                             = (int) 1000000007;
const int   max_prime                       = (int) 1e6+3;
//data_type_compressions :
#define     ll                              long long int
#define     ull                             unsigned long long int 
#define     ui                              unsigned int 
//STL : 
#define     vi                              vector<int>
#define     vs                              vector<string>
#define     vl                              vector<ll>
#define     si                              set<int>
#define     sl                              set<ll>
#define     ip                              pair<int,int>
#define     lp                              pair<ll,ll>
#define     ips                             pair<string,int>
#define     lps                             pair<string,ll>
#define     ipc                             pair<char,int>
#define     lpc                             pair<char,ll>
#define     vip                             vector<ip>
#define     pq                              priority_queue
#define     vlp                             vector<lp>
#define     hashmap                         unordered_map
#define     msi                             multiset<int>
#define     msl                             multiset<ll>
#define     pb                              push_back
#define     spb                             insert
#define     all(x)                          x.begin(),x.end()
#define     all_0(x)                        memset(x,0,sizeof(x))
#define     all_neg(x)                      memset(x,-1,sizeof(x))
#define     all_1(x)                        memset(x,1,sizeof(x))
//I/O : 
#define     Faster                          ios_base::sync_with_stdio(false);cin.tie(NULL);
#define     Read(x)                         freopen(x,"r",stdin)
#define     Write(x)                        freopen (x,"w",stdout)
#define     PI                              acos(-1)
#define     take_the_array(x,n)             for(int i=0;i<n;i++){scanf("%d",&a[i]);}
#define     take_the_matrix(x,n,m)          for(int i=0;i<n;i++){for(int j=0;j<m;j++){scanf("%d",&x[i][j]);}}
#define     take_the_vector(x,n)            int o;for(int i=0;i<n;i++){scanf("%d",&o);x.pb(o);};
#define     print_the_array(x,n)            for(int i=0;i<n;i++){printf("%d ",x[i]);}printf("\n");
#define     print_the_vector(x)             for(auto ii:x){cout<<ii<<" ";}cout<<endl;
#define     print_the_matrix(x,n,m)         for(int i=0;i<n;i++){for(int j=0;j<m;j++){printf("%d ",x[i][j]);}printf("\n");}
#define     int_in(x)                       scanf("%d",&x);
#define     ll_in(x)                        scanf("%lld",&x);
#define     char_in(x)                      scanf("%c",&x);
#define     str_in(x)                       scanf("%s",&x);
#define     int_out(x)                      printf("%d ",x);
#define     ll_out(x)                       printf("%lld",x);
#define     char_out(x)                     printf("%c",x);
#define     str_out(x)                      printf("%s",x);
#define     YES                             printf("YES\n");
#define     Yes                             printf("Yes\n");
#define     NO                              printf("NO\n");
#define     No                              printf("No\n");
#define     nl                              printf("\n");
//extras :
#define     e5                              int main()
#define     checkmate                       return 0;
#define     INF                             0x3f
#define     in_range(x,y)                   for(int i=x;i<=y;i++)
#define     in_range_back(x,y)              for(int i=y;i>=x;i--)
#define     in_range_nested(x,y)            for(int j=x;j<=y;j++)
// MATH functions : 
bool is_prime(ll n){ll x=sqrt(n);bool check=1;for(ll i=x;i>=2;i--){if(n%i==0){check=0;break;}}return check;}
int first_set_bit(int n){return log2(n & -n)+1;}
//sieve : 
bool mark[N];
vector<int>primes;
void sieve(){memset(mark,true,sizeof(mark));mark[0]=0,mark[1]=0,mark[2]=1;for(int i=4;i<=N;i+=2){mark[i]=0;}
for(int i=3;i*i<=N;i+=2){if(mark[i]){for(int j=i*i;j<=N;j+=i){mark[j]=0;}}}primes.push_back(2);
for(int i=3;i<=N;i+=2){if(mark[i]){primes.push_back(i);}}}
void seg_sieve(int l,int r){if(l==1)l++;int range=r-l+1;bool prime[range]={false};
for(int i=0;primes[i]*primes[i]<=r;i++){int p=(l/primes[i])*primes[i];if(p<l)p+=primes[i];
for(;p<=r;p+=primes[i]){if(p!=primes[i]){prime[p-l]=true;}}}}
//......sieve ends...
ll loop_phi(ll n){ll result=n, p=2;while(p*p<=n){if(n%p==0){while(n%p==0) n/=p;result-=result/p;}
p++;}if(n>1) result-=result/n;return result;}
ll phi[N];
void sieve_phi(){for(int i=1;i<=N;i++){phi[i]=i;}for(int i=2;i<=N;i++){if(phi[i]==i){phi[i]=i-1;
for(int j=2*i;j<=N;j+=i){phi[j]=(phi[j]/i)*(i-1);}}}}
long long gcd(long long int a, long long int b){if (b == 0)return a;return gcd(b, a % b);}
int nodivs(ll n){int cnt=0;for(ll i=1;i*i<=n;i++){if(n%i==0){cnt++;if(n/i!=i){cnt++;}}}return cnt;}
long long lcm(int a, int b){return (a / gcd(a, b)) * b;}
bool perfect_square(ll val){double s=sqrt(val);ll i=sqrt(val);if(s==i){return 1;}else{return 0;}}
/*You,know, "MATH NEVER ENDS"...so on....*/
//fenwick_tree :
void update(int arr[],int indx,int arr_size,int val){while(indx<arr_size){arr[indx]+=val;indx+=(indx&(-indx));}}
ll get_sum(int arr[],int indx,int arr_size){ll sum=0;while(indx>=0){sum+=arr[indx];indx-=(indx&(-indx));}}
//DSU :
int root[N],root_size[N];int find_root(int x){while(root[x]!=x){x=root[x];}return x;}
void unite(int x,int y){x=find_root(x),y=find_root(y);if(x==y){return;}else{if(root_size[x]>root_size[y]){
root_size[x]+=root_size[y];root[y]=x;}else{root_size[y]+=root_size[x];root[x]=y;}}}
//DFS ; 
bool visited[N];vi adj[N];void dfs(int u){if(visited[u]){return;}visited[u]=true;for(auto x:adj[u]){dfs(x);}}
bool cycle_detection(int node,int parent){visited[node]=true;for(auto child:adj[node]){
if(!visited[child]){if(cycle_detection(child,node)==true){return true;}}else{if(child!=parent){return true;}}}return false;}
int col[N];bool bipartite_test(int u,int c){visited[u]=true;col[u]=c;for(auto child:adj[u]){
if(!visited[child]){if(bipartite_test(child,c^1)==false){return false;}}else{if(col[child]==col[u]){return false;}}}return true;}
//BFS : 
int dist[N];void bfs(int source){queue<int>q;q.push(source);visited[source]=true;dist[source]=0;while(!q.empty()){
int curr=q.front();q.pop();for(auto child:adj[curr]){if(!visited[child]){q.push(child);visited[child]=true;dist[child]=dist[curr]+1;}}}}
ll char_to_num(char *str){ll num=0,n=strlen(str),r;for(int i=0;i<n;i++){if(str[i]>47 && str[i]<58){
if(i==0){num=(str[i]-48);if(n>1 && str[i+1]>47 && str[i+1]<58){num*=10;}}
else{num+=(str[i]-48);if(i!=n-1 && str[i+1]>47 && str[i+1]<58){num*=10;}}}}return num;}
char *num_to_char(ll num,char *str){ll r,i=0,n=num,j;while(1){r=n%10;str[i]=(48+r);
n/=10;if(n==0){break;}i++;}str[i+1]='\0';for(i=0,j=strlen(str)-1;i<strlen(str)/2;i++,j--){swap(str[i],str[j]);}return str;}

bool desc(int a,int b){return a>b;}bool cond(int a,int b){/*sorting_conditions*/}

//..............................................Let's Start with e5, shan't we.?......................................

e5{
    int n,m,i,j;
    int_in(n)int_in(m)
    map<int,vip>record;
    map<int,int>indx;
    map<int,int>ans;
    in_range(1,n){int u;int_in(u);indx[u]=i;}
    in_range(1,n){root[i]=i;root_size[i]=1;}
    in_range(1,m){int u,v;int_in(u)int_in(v)unite(u,v);}
    in_range(1,n){root[i]=find_root(i);}
    in_range(1,n){record[root[i]].pb({i,indx[i]});}
    in_range(1,n){
        if(record[i].empty()){continue;}
        vi temp,val;
        for(auto x:record[i]){temp.pb(x.second);val.pb(x.first);}
        sort(all(temp));
        sort(all(val),greater<int>());
        in_range_nested(0,record[i].size()-1){ans[temp[j]]=val[j];}
        //cout<<record[i].size()<<endl;
    }
    in_range(1,n){int_out(ans[i]);}nl
    checkmate
}


 	 	  	 	  	 				   	 		 	  			