#include <bits/stdc++.h>
using namespace std;
vector<int> v;
void DFS(int u, vector<int> adj[], vector<bool> &vis) {
  v.push_back(u);
  vis[u] = true;
  for (long long i = 0; i < adj[u].size(); i++)
    if (!vis[adj[u][i]]) DFS(adj[u][i], adj, vis);
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int t = 1;
  while (t--) {
    int n, m;
    cin >> n >> m;
    int per[n + 1], ans[n + 1];
    for (long long i = 1; i <= n; i++) cin >> per[i];
    vector<int> adj[n + 1];
    for (long long i = 0; i < m; i++) {
      int a, b;
      cin >> a >> b;
      adj[a].push_back(b);
      adj[b].push_back(a);
    };
    vector<bool> vis(n + 1, false);
    for (long long i = 1; i <= n; i++) {
      if (vis[i] == false) {
        DFS(i, adj, vis);
        vector<int> temp;
        for (long long j = 0; j < v.size(); j++) temp.push_back(per[v[j]]);
        sort(temp.begin(), temp.end(), greater<int>());
        sort(v.begin(), v.end());
        for (long long j = 0; j < v.size(); j++) ans[v[j]] = temp[j];
        v.clear();
      }
    }
    for (long long i = 1; i <= n; i++) cout << ans[i] << " ";
  }
}
