#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 5;
int n, m, p[N], f[N], pos[N];
vector<int> vec[N];
int find(int u) { return u == f[u] ? u : f[u] = find(f[u]); }
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; i++) scanf("%d", p + i), f[i] = i;
  for (int u, v, i = 1; i <= m; i++) {
    scanf("%d%d", &u, &v);
    u = find(u);
    v = find(v);
    if (u != v) f[v] = u;
  }
  for (int i = 1; i <= n; i++) vec[find(i)].push_back(p[i]);
  for (int i = 1; i <= n; i++)
    if (f[i] == i) {
      sort(vec[i].begin(), vec[i].end(), greater<int>());
    }
  for (int i = 1; i <= n; i++) {
    printf("%d%c", vec[f[i]][pos[f[i]]++], " \n"[i == n]);
  }
  return 0;
}
