#include <bits/stdc++.h>
const int mod = 1e9 + 7;
const int MOD = 998244353;
inline int add(int x, int y) {
  x += y;
  if (x >= mod) x -= mod;
  return x;
}
inline int sub(int x, int y) {
  x -= y;
  if (x < 0) x += mod;
  return x;
}
inline int mul(int x, int y) { return (x * 1ll * y) % mod; }
using namespace std;
const long long int maxn = 1e6 + 10;
const long long int inf = 1e9;
const double pi = acos(-1);
long long int n, m, u, v, a, b, c, d, p, q, x, y, z, minn, maxx;
long long int parent[maxn], rnk[maxn], perm[maxn];
set<long long int> st[maxn];
long long int find_set(long long int v) {
  if (v == parent[v]) return v;
  return parent[v] = find_set(parent[v]);
}
void init_set() {
  for (long long int v = 1; v <= n; v++) {
    parent[v] = v;
    rnk[v] = 1;
  }
}
void union_sets(long long int a, long long int b) {
  a = find_set(a);
  b = find_set(b);
  if (a != b) {
    if (rnk[a] < rnk[b]) {
      parent[a] = b;
      rnk[b] += rnk[a];
      st[b].insert(st[a].begin(), st[a].end());
      st[a].clear();
    } else {
      parent[b] = a;
      rnk[a] += rnk[b];
      st[a].insert(st[b].begin(), st[b].end());
      st[b].clear();
    }
  }
}
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie();
  cout.tie();
  cin >> n >> m;
  init_set();
  for (long long int i = 1; i <= n; i++) {
    cin >> perm[i];
    st[i].insert(perm[i]);
  }
  for (long long int i = 1; i <= m; i++) {
    cin >> u >> v;
    union_sets(u, v);
  }
  for (long long int i = 1; i <= n; i++) {
    parent[i] = find_set(i);
    x = parent[i];
    cout << *st[x].rbegin() << " ";
    auto it = st[x].end();
    it--;
    st[x].erase(it);
  }
  return 0;
}
