#include <bits/stdc++.h>
using namespace std;
class Graph {
 public:
  int num_vertices;
  vector<int> vertex;
  vector<int> *adj;
  Graph(int n) {
    num_vertices = n;
    adj = new vector<int>[n + 1];
    vertex.resize(n + 1);
  }
  void addVertex(int i, int x) { vertex[i] = x; }
  void addEdge(int x, int y) {
    adj[x].push_back(y);
    adj[y].push_back(x);
  }
  void DFS_visit(int current, vector<bool> &visited, vector<int> &index,
                 vector<int> &value) {
    visited[current] = true;
    index.push_back(current);
    value.push_back(vertex[current]);
    for (int neighbor : adj[current]) {
      if (!visited[neighbor]) {
        DFS_visit(neighbor, visited, index, value);
      }
    }
  }
  void DFS() {
    vector<bool> visited(num_vertices + 1, false);
    vector<int> index;
    vector<int> value;
    for (int i = 1; i < num_vertices + 1; i++) {
      if (!visited[i]) {
        index.clear();
        value.clear();
        DFS_visit(i, visited, index, value);
        sort(index.begin(), index.end());
        sort(value.rbegin(), value.rend());
        for (int j = 0; j < index.size(); j++) vertex[index[j]] = value[j];
      }
    }
  }
};
int main() {
  ios::sync_with_stdio(false);
  int n, m, x, y;
  cin >> n >> m;
  Graph G(n);
  for (int i = 1; i <= n; i++) {
    cin >> x;
    G.addVertex(i, x);
  }
  for (int i = 0; i < m; i++) {
    cin >> x >> y;
    G.addEdge(x, y);
  }
  G.DFS();
  copy(G.vertex.begin() + 1, G.vertex.end(), ostream_iterator<int>(cout, " "));
  cout << endl;
  return 0;
}
