#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 5;
int a[N], vis[N];
vector<vector<int>> adj(N);
int32_t main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int n, m;
  cin >> n >> m;
  for (int i = 0; i < n; ++i) cin >> a[i + 1];
  int x, y;
  for (int i = 0; i < m; ++i) {
    cin >> x >> y;
    adj[x].push_back(y), adj[y].push_back(x);
  }
  for (int i = 1; i <= n; ++i) {
    if (vis[i]) continue;
    vis[i] = 1;
    queue<int> q;
    q.push(i);
    vector<int> pos, val;
    pos.push_back(i), val.push_back(a[i]);
    while (!q.empty()) {
      int p = q.front();
      q.pop();
      for (int tt : adj[p])
        if (!vis[tt]) {
          vis[tt] = 1;
          q.push(tt);
          pos.push_back(tt), val.push_back(a[tt]);
        }
    }
    sort(pos.begin(), pos.end()), sort(val.rbegin(), val.rend());
    for (int j = 0; j < (int)pos.size(); ++j) a[pos[j]] = val[j];
  }
  for (int i = 0; i < n; ++i) cout << a[i + 1] << ' ';
  return 0;
}
