#include <bits/stdc++.h>
using namespace std;
long long n, m, nc = 0, a, b;
vector<vector<long long>> adj(1000005), comp1(1000005), comp2(1000005);
vector<long long> vis(1000005, 0), p(1000005);
void dfs(long long v) {
  vis[v] = 1;
  comp1[nc].push_back(v);
  comp2[nc].push_back(p[v]);
  for (long long j = 0; j < adj[v].size(); j++) {
    if (!vis[adj[v][j]]) dfs(adj[v][j]);
  }
}
int main(void) {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> n >> m;
  for (long long i = 1; i <= n; i++) cin >> p[i];
  for (long long i = 1; i <= m; i++) {
    cin >> a >> b;
    adj[a].push_back(b);
    adj[b].push_back(a);
  }
  for (long long i = 1; i <= n; i++) {
    if (!vis[i]) {
      nc++;
      dfs(i);
    }
  }
  for (long long i = 1; i <= nc; i++) {
    sort(comp1[i].begin(), comp1[i].end());
    sort(comp2[i].begin(), comp2[i].end());
    for (long long it1 = 0, it2 = comp1[i].size() - 1; it1 < comp1[i].size();
         it1++, it2--)
      p[comp1[i][it1]] = comp2[i][it2];
  }
  for (long long i = 1; i <= n; i++) cout << p[i] << " ";
  return 0;
}
