#include <bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f;
void Open() {}
const int N = 1000050;
int id1[N];
vector<int> id2[N];
int a[N], pa[N];
int ans[N];
int find(int x) { return pa[x] == x ? x : pa[x] = find(pa[x]); }
int cmp(int i, int j) { return a[i] > a[j]; }
int main() {
  int n, m;
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; i++) scanf("%d", &a[i]), pa[i] = i;
  for (int i = 0; i < m; i++) {
    int u, v;
    scanf("%d%d", &u, &v);
    u = find(u), v = find(v);
    pa[u] = v;
  }
  for (int i = 1; i <= n; i++) id2[find(i)].push_back(i);
  for (int i = 1; i <= n; i++) {
    int t = 0;
    for (int j = 0; j < id2[i].size(); j++) {
      id1[t++] = id2[i][j];
    }
    if (t == 0) continue;
    sort(id1, id1 + t, cmp);
    for (int j = 0; j < t; j++) {
      ans[id2[i][j]] = a[id1[j]];
    }
  }
  for (int i = 1; i <= n; i++) printf("%d%c", ans[i], " \n"[i == n]);
  return 0;
}
