#include <bits/stdc++.h>
using namespace std;
vector<int> pos, val, output(1000001, -1), input(1000001, 0),
    visited(1000001, 0), g[1000001];
void dfs(int src) {
  visited[src] = true;
  pos.push_back(src);
  val.push_back(input[src]);
  for (auto i : g[src]) {
    if (visited[i] == false) {
      dfs(i);
    }
  }
}
int main() {
  int n, m;
  cin >> n >> m;
  for (int i = 1; i <= n; i++) {
    cin >> input[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++) {
    if (visited[i] == false) {
      dfs(i);
      sort(pos.begin(), pos.end());
      sort(val.begin(), val.end(), greater<int>());
      for (int i = 0; i < pos.size(); i++) {
        output[pos[i]] = val[i];
      }
      pos.clear();
      val.clear();
    }
  }
  for (int i = 1; i <= n; i++) {
    if (output[i] == -1) {
      output[i] = input[i];
    }
  }
  for (int i = 1; i <= n; i++) {
    cout << output[i] << " ";
  }
}
