#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 10;
int n, m, a[N], visit[N];
vector<int> adj[N];
vector<int> p;
void dfs(int node) {
  if (visit[node]) return;
  p.push_back(node);
  visit[node] = 1;
  for (int x : adj[node]) dfs(x);
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cin >> n >> m;
  for (int i = 0; i < (int)(n); ++i) cin >> a[i], --a[i];
  for (int i = 0; i < (int)(m); ++i) {
    int u, v;
    cin >> u >> v;
    --u, --v;
    adj[u].push_back(v);
    adj[v].push_back(u);
  }
  for (int i = 0; i < (int)(n); ++i) {
    if (!visit[i]) {
      p.clear();
      dfs(i);
      vector<int> p2;
      for (int x : p) p2.push_back(a[x]);
      sort(p2.begin(), p2.end());
      sort(p.begin(), p.end());
      for (int x : p) {
        a[x] = p2.back();
        p2.pop_back();
      }
    }
  }
  for (int i = 0; i < (int)(n); ++i) cout << a[i] + 1 << " ";
  cout << "\n";
  return 0;
}
