#include <bits/stdc++.h>
using namespace std;
struct permuutation {
  int a;
  int id;
};
struct permuutation a[1000040];
int fa[1000040];
int temp[1000040];
int ID[1000040];
int aws[1000040];
int a1[1000040];
vector<permuutation> v[1000040];
bool cnp(struct permuutation A, struct permuutation B) { return A.a < B.a; }
int finds(int x) {
  if (fa[x] != x) fa[x] = finds(fa[x]);
  return fa[x];
}
int main() {
  int n, m;
  scanf("%d%d", &n, &m);
  for (int i(1); i <= n; i++) {
    scanf("%d", &a[i].a);
    a[i].id = i;
    fa[i] = i;
  }
  for (int i(1); i <= m; i++) {
    int p, q;
    scanf("%d%d", &p, &q);
    p = finds(p);
    q = finds(q);
    if (p != q) fa[p] = q;
  }
  int cnt = 0;
  for (int i(1); i <= n; i++) {
    int x = finds(i);
    if (x == i) temp[++cnt] = x;
    v[x].push_back(a[i]);
  }
  for (int i1(1); i1 <= cnt; i1++) {
    int i = temp[i1];
    int su = v[i].size();
    for (int j(0); j < su; j++) {
      ID[j] = v[i][j].id;
      a1[j] = v[i][j].a;
    }
    sort(a1, a1 + su);
    sort(ID, ID + su);
    for (int j(0); j < su; j++) aws[ID[j]] = a1[su - 1 - j];
  }
  for (int i(1); i <= n; i++) printf("%d ", aws[i]);
  printf("\n");
  return 0;
}
