#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e6 + 2;
priority_queue<int> Q[maxn];
int num[maxn];
int fa[maxn];
int find(int x) { return fa[x] == x ? x : fa[x] = find(fa[x]); }
int main() {
  int n, m;
  cin >> n >> m;
  for (int i = 1; i <= n; i++) {
    scanf("%d", &num[i]);
    fa[i] = i;
  }
  for (int i = 0; i < m; i++) {
    int u, v;
    scanf("%d %d", &u, &v);
    fa[find(u)] = find(v);
  }
  for (int i = 1; i <= n; i++) {
    find(i);
    Q[fa[i]].push(num[i]);
  }
  for (int i = 1; i <= n; i++) {
    printf("%d ", Q[fa[i]].top());
    Q[fa[i]].pop();
  }
  return 0;
}
