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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 

#include <bits/stdc++.h>
using namespace std;
#define INF 4e18
#define N 1000005
#define FOR(n) for(ll i=0;i<n;++i)
#define FORD(i,a,b) for(ll i=a;i>=b;--i)
#define pb push_back
#define mp make_pair
#define all(x) x.begin(),x.end()
#define ll long long int
#define s(x) scanf("%d", &x)
#define sd(x) scanf("%lf", &x)
#define sl(x) scanf("%lld", &x)
#define mod 998244353
#define fi first
#define se second
#define p(x) printf("%d",x)
#define pl(x) printf("%lld", x)
#define pd(x) printf("%lf", x)
#define pn() printf("\n")
#define vi vector<ll>
#define vl(v) vector<ll ll>v
#define pii pair<ll,ll>
#define len(s) s.length()
#define start_code ios_base::sync_with_stdio(0)
void input(ll n,ll a[]){FOR(n) cin>>a[i];}
ll bin_expo(ll A, ll B, ll M){ll res = 1LL;while (B > 0){if(B % 2 == 1){res = (res * A) % M;}A = (A * A) % M; B = B / 2;}return res;}
#define LOGN 20

ll gcd(ll a,ll b)
{
    if (b == 0)
        return a;
    return gcd(b, a % b);
}
ll lcm(ll a, ll b)
{
    return (a*b)/gcd(a, b);
}
double distance(ll x1, ll y1, ll x2, ll y2)
{
    return sqrt(pow(x2 - x1, 2) + pow(y2 - y1, 2) * 1.0);
}
ll par[N],sz[N];

ll find(ll u)
{
	if(par[u] != u)
		return par[u] = find(par[u]);
	return u;
}
int main()
{
	start_code;
	ll n,m1,x,y,pa;
	cin>>n>>m1;
	ll a[n];
	input(n,a);
	FOR(n)
	{
		par[i] = i;
		sz[i] = 1;
	}
	cout<<"hello"<<endl;
	FOR(m1)
	{
		cin>>x>>y;
		x--;y--;
		ll t1 = find(x),t2 =find(y);
		if(t1!=t2)
		{
			if(sz[t1] > sz[t2])
				swap(t1,t2);
			par[t1] = t2;
			sz[t2] += sz[t1];
		}
	}
	//cout<<"hello"<<endl;
	set<ll> s;
	unordered_map<ll,ll> m;
	FOR(n)
	{
	//	cout<<find(i)<<" ";
		s.insert(find(i));
	}
//	cout<<"hello"<<endl;
	ll c=0;
	for(auto it:s)
	{
//		cout<<it<<" ";
		m[it] = c;
		c++;
	}
//	cout<<endl;
	vector<ll> v[c];
	FOR(n)
	{
		v[m[find(a[i]-1)]].pb(a[i]-1);
	}
	ll till[c] = {0};
	FOR(c)
	{
		sort(v[i].rbegin(),v[i].rend());
	}
	ll ans[n];
	FOR(n)
	{
		pa = find(a[i]-1);
		ans[i] = v[m[pa]][till[m[pa]]++];
	}
	FOR(n)
	{
		cout<<ans[i]+1<<" ";
	}
	cout<<endl;
	return 0;
}