#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 5;
priority_queue<int> q[N];
int fa[N];
int n;
void init() {
  for (int i = 0; i <= n; i++) {
    fa[i] = i;
    while (!q[i].empty()) q[i].pop();
  }
}
int find(int x) {
  if (fa[x] != x) fa[x] = find(fa[x]);
  return fa[x];
}
void merge(int a, int b) {
  a = find(a);
  b = find(b);
  if (a != b) fa[a] = fa[b];
}
int tar[N];
int main() {
  int m;
  while (scanf("%d%d", &n, &m) == 2) {
    init();
    memset(tar, 0, sizeof(tar));
    for (int i = 1; i <= n; i++) scanf("%d", tar + i);
    for (int i = 0; i < m; i++) {
      int u, v;
      scanf("%d%d", &u, &v);
      merge(u, v);
    }
    for (int i = 1; i <= n; i++) q[find(i)].push(tar[i]);
    for (int i = 1; i <= n; i++) {
      int temp = q[find(i)].top();
      q[find(i)].pop();
      printf("%d", temp);
      i == n ? printf("\n") : printf(" ");
    }
  }
  return 0;
}
