#include <bits/stdc++.h>
using namespace std;
set<int> s[1000005];
int v[1000005] = {0};
int ci[1000005] = {0};
int f(int x) { return ci[x] == x ? x : ci[x] = f(ci[x]); }
int main() {
  int n, m;
  cin >> n >> m;
  int group = 1, tg = 0;
  int i;
  for (i = 1; i <= n; i++) cin >> v[i];
  for (i = 1; i <= n; i++) ci[i] = i;
  for (i = 0; i < m; i++) {
    int a, b;
    scanf("%d %d", &a, &b);
    int ta = f(a), tb = f(b);
    if (ta != tb) ci[ta] = tb;
  }
  for (i = 1; i <= n; i++) {
    s[f(i)].insert(v[i]);
  }
  for (i = 1; i <= n; i++) {
    if (i != 1) putchar(' ');
    int te = ci[i];
    cout << *(--s[te].end());
    s[te].erase(--s[te].end());
  }
  cout << endl;
  return 0;
}
