#include <bits/stdc++.h>
using namespace std;
vector<int> g[(int)1e6 + 6];
bool visited[(int)1e6 + 6];
int a[(int)1e6 + 6];
vector<int> s, t;
void dfs(int i) {
  if (!visited[i]) {
    s.push_back(a[i]);
    t.push_back(i);
    visited[i] = true;
    for (auto j : g[i]) dfs(j);
  }
  return;
}
int main() {
  std::ios::sync_with_stdio(false);
  cin.tie(0);
  int n, m;
  cin >> n >> m;
  for (int i = 1; i <= n; i++) cin >> a[i];
  for (int i = 0; i < m; i++) {
    int u, v;
    cin >> u >> v;
    g[u].push_back(v);
    g[v].push_back(u);
  }
  for (int i = 1; i <= n; i++) {
    dfs(i);
    sort(s.begin(), s.end(), greater<int>());
    sort(t.begin(), t.end());
    auto it = s.begin();
    for (auto i : t) {
      a[i] = *it;
      it++;
    }
    s.clear();
    t.clear();
  }
  for (int i = 1; i <= n; i++) cout << a[i] << ' ';
  cout << '\n';
  return 0;
}
