#include <bits/stdc++.h>
using namespace std;
long long com = 0;
long long n, m;
void DFS_visit(vector<long long> adj[], long long vis[], long long comp[],
               long long u) {
  vis[u] = 1;
  comp[u] = com;
  for (long long i = 0; i < adj[u].size(); i++) {
    if (vis[adj[u][i]] == 0) DFS_visit(adj, vis, comp, adj[u][i]);
  }
  return;
}
void DFS(vector<long long> adj[], long long vis[], long long comp[]) {
  for (long long i = 0; i < n; i++) {
    if (vis[i] == 0) {
      com++;
      DFS_visit(adj, vis, comp, i);
    }
  }
  return;
}
int main() {
  cin >> n >> m;
  long long arr[n], ans[n];
  for (long long i = 0; i < n; i++) {
    cin >> arr[i];
  }
  vector<long long> adj[n];
  long long a, b;
  for (long long i = 0; i < m; i++) {
    cin >> a >> b;
    a--, b--;
    adj[a].push_back(b);
    adj[b].push_back(a);
  }
  long long comp[n], vis[n];
  for (long long i = 0; i < n; i++) {
    comp[i] = 0;
    vis[i] = 0;
  }
  DFS(adj, vis, comp);
  vector<long long> nodes[com + 1];
  for (long long i = 0; i < n; i++) {
    nodes[comp[i]].push_back(arr[i]);
  }
  for (long long i = 1; i <= com; i++) {
    sort(nodes[i].begin(), nodes[i].end());
  }
  long long ind[com + 1];
  for (long long i = 0; i < com + 1; i++) {
    ind[i] = nodes[i].size() - 1;
  }
  for (long long i = 0; i < n; i++) {
    ans[i] = nodes[comp[i]][ind[comp[i]]];
    ind[comp[i]]--;
  }
  for (long long i = 0; i < n; i++) {
    cout << ans[i] << ' ';
  }
  cout << endl;
  return 0;
}
