#include <bits/stdc++.h>
using namespace std;
const int N = 1000006;
int head[N], cnt;
struct edge {
  int n, t;
  edge(int a = 0, int b = 0) : n(a), t(b) {}
} e[N << 1];
void ins(int a, int b) {
  e[cnt] = edge(head[a], b);
  head[a] = cnt++;
}
int n, m, in[N], ans[N], t1[N], t2[N], cc;
bool vis[N];
queue<int> que;
void solve(int cur) {
  int i, u, v;
  while (!que.empty()) que.pop();
  que.push(cur);
  vis[cur] = 1;
  cc = 1;
  t1[0] = cur;
  t2[0] = in[cur];
  while (!que.empty()) {
    u = que.front();
    que.pop();
    for (i = head[u]; i != -1; i = e[i].n) {
      v = e[i].t;
      if (vis[v]) continue;
      vis[v] = 1;
      que.push(v);
      t1[cc] = v;
      t2[cc++] = in[v];
    }
  }
  sort(t1, t1 + cc);
  sort(t2, t2 + cc, greater<int>());
  for (i = 0; i < cc; ++i) ans[t1[i]] = t2[i];
}
int main() {
  memset(head, -1, sizeof(head));
  scanf("%d%d", &n, &m);
  int i, j, a, b;
  for (i = 1; i <= n; ++i) scanf("%d", in + i);
  for (i = 1; i <= m; ++i) {
    scanf("%d%d", &a, &b);
    ins(a, b);
    ins(b, a);
  }
  for (i = 1; i <= n; ++i)
    if (!vis[i]) solve(i);
  for (i = 1; i <= n; ++i) printf("%d%c", ans[i], i == n ? '\n' : ' ');
  return 0;
}
