#include <bits/stdc++.h>
using namespace std;
void solve() {
  int n, m;
  cin >> n >> m;
  vector<int> arr(n + 1);
  for (int i = 1; i <= n; i++) {
    cin >> arr[i];
  }
  int u, v;
  vector<vector<int>> graph(n + 1);
  for (int i = 0; i < m; i++) {
    cin >> u >> v;
    graph[u].push_back(v);
    graph[v].push_back(u);
  }
  vector<bool> visited(n + 1, false);
  vector<int> store;
  function<void(int)> dfs = [&](int u) {
    visited[u] = true;
    store.push_back(u);
    for (auto item : graph[u]) {
      if (!visited[item]) {
        dfs(item);
      }
    }
  };
  for (int i = 1; i <= n; i++) {
    if (!visited[i]) {
      store.clear();
      dfs(i);
      vector<int> values;
      for (auto item : store) {
        values.push_back(arr[item]);
      }
      sort(store.begin(), store.end());
      sort(values.rbegin(), values.rend());
      for (int k = 0; k < store.size(); k++) {
        arr[store[k]] = values[k];
      }
    }
  }
  for (int i = 1; i <= n; i++) {
    cout << arr[i] << " ";
  }
  cout << endl;
}
int main() {
  int t = 1;
  while (t--) {
    solve();
  }
}
