#include <bits/stdc++.h>
using namespace std;
int n, q;
int ans[1000005], a[1000005], p[1000005], ace[1000005];
vector<int> gr[1000005], pos_gr[1000005];
int f(int u) {
  if (p[u] == u) return u;
  return p[u] = f(p[u]);
}
void m(int u, int v) { p[f(u)] = f(v); }
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cin >> n >> q;
  int u, v;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    p[i] = i;
  }
  for (int i = 1; i <= q; i++) {
    cin >> u >> v;
    m(u, v);
  }
  for (int i = 1; i <= n; i++) ace[i] = f(i);
  for (int i = 1; i <= n; i++) gr[ace[i]].push_back(a[i]);
  for (int i = n; i >= 1; i--) pos_gr[ace[i]].push_back(i);
  for (int i = 1; i <= n; i++) {
    sort(gr[i].begin(), gr[i].end());
    for (int j = gr[i].size() - 1; j >= 0; j--) ans[pos_gr[i][j]] = gr[i][j];
  }
  for (int i = 1; i <= n; i++) printf("%d ", ans[i]);
}
