#include <bits/stdc++.h>
using namespace std;
const int D = 1000005, MOD = 1000000007;
int fa[D], a[D], t[D];
int head[D];
int to[D], linkto[D], tot = 0;
int tmp[D], el[D];
int getfa(int x) {
  if (fa[x] == x)
    return fa[x];
  else
    return fa[x] = getfa(fa[x]);
}
int samefa(int x, int y) { return getfa(x) == getfa(y); }
void merge(int x, int y) {
  int fax = getfa(x), fay = getfa(y);
  fa[fax] = fay;
}
void ins(int pos, int ele) {
  to[tot] = ele;
  linkto[tot] = head[pos];
  head[pos] = tot++;
}
bool cmp0(const int& a, const int& b) { return a < b; }
bool cmp1(const int& a, const int& b) { return t[a] > t[b]; }
int main() {
  int i, x, y, n, m, j;
  for (i = 0; i < D; i++) fa[i] = i;
  memset(head, -1, sizeof(head));
  scanf("%d%d", &n, &m);
  for (i = 0; i < n; i++) {
    scanf("%d", a + i);
    t[i] = a[i];
  }
  for (i = 0; i < m; i++) {
    scanf("%d%d", &x, &y);
    x--, y--;
    merge(x, y);
  }
  for (i = 0; i < n; i++) {
    fa[i] = getfa(i);
    ins(fa[i], i);
  }
  for (i = 0; i < n; i++) {
    int total = 0;
    for (j = head[i]; j != -1; j = linkto[j]) {
      tmp[total] = el[total] = to[j];
      total++;
    }
    sort(tmp, tmp + total, cmp0);
    sort(el, el + total, cmp1);
    for (j = 0; j < total; j++) a[tmp[j]] = t[el[j]];
  }
  for (i = 0; i < n; i++) printf("%d ", a[i]);
  printf("\n");
  return 0;
}
