#include <bits/stdc++.h>
using namespace std;
int n, m;
int a[1000005];
vector<int> G[1000005];
int ans[1000005];
vector<int> fir;
vector<int> sec;
bool vis[1000005];
void dfs(int u) {
  if (vis[u]) return;
  vis[u] = 1;
  fir.push_back(u);
  sec.push_back(a[u]);
  for (int i = 0; i < (int)G[u].size(); i++) {
    int v = G[u][i];
    dfs(v);
  }
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
  for (int i = 0; i < m; i++) {
    int x, y;
    scanf("%d%d", &x, &y);
    G[x].push_back(y);
    G[y].push_back(x);
  }
  for (int i = 1; i <= n; i++) {
    if (vis[i]) continue;
    fir.clear();
    sec.clear();
    dfs(i);
    sort(fir.begin(), fir.end());
    sort(sec.begin(), sec.end());
    int k = 0;
    for (int j = (int)sec.size() - 1; j >= 0; j--) {
      ans[fir[k++]] = sec[j];
    }
  }
  for (int i = 1; i <= n; i++) {
    if (i != 1) printf(" ");
    printf("%d", ans[i]);
  }
  printf("\n");
  return 0;
}
