#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 5;
vector<int> ans[N];
vector<int> root;
int pos[N], par[N], a[N], vis[N];
int Find(int n) {
  if (n == par[n]) return n;
  return par[n] = Find(par[n]);
}
int main() {
  int n, m, u, v;
  scanf("%d %d", &n, &m);
  for (int i = 1; i <= n; ++i) {
    scanf("%d", a + i);
    par[i] = i;
  }
  while (m--) {
    scanf("%d %d", &u, &v);
    u = Find(u), v = Find(v);
    if (u != v) par[u] = v;
  }
  for (int i = 1; i <= n; ++i) {
    int u = Find(i);
    ans[u].push_back(a[i]);
    if (!vis[u]) {
      root.push_back(u);
      vis[u] = true;
    }
  }
  for (int i = 0; i < root.size(); ++i)
    sort(ans[root[i]].rbegin(), ans[root[i]].rend());
  for (int i = 1; i < n; ++i) printf("%d ", ans[Find(i)][pos[Find(i)]++]);
  printf("%d\n", ans[Find(n)][pos[Find(n)]]);
  return 0;
}
