#include <bits/stdc++.h>
using namespace std;
unsigned long long pw[67];
void pre() {
  pw[0] = 1;
  for (int i = 1; i < 64; i++) pw[i] = 2 * pw[i - 1];
}
bool vis[1000005];
vector<int> v[1000005];
vector<int> foo;
void dfs(int start) {
  vis[start] = true;
  foo.push_back(start);
  for (int i = 0; i < v[start].size(); i++) {
    if (!vis[v[start][i]]) {
      dfs(v[start][i]);
    }
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  pre();
  vector<int> bar;
  int ans[1000005];
  int n, m, i, p, q, s[1000005];
  for (i = 0; i < 1000005; i++) {
    vis[i] = false;
  }
  cin >> n >> m;
  for (i = 1; i <= n; i++) {
    cin >> s[i];
  }
  for (i = 0; i < m; i++) {
    cin >> p >> q;
    v[p].push_back(q);
    v[q].push_back(p);
  }
  for (i = 1; i <= n; i++) {
    if (!vis[i]) {
      foo.erase(foo.begin(), foo.end());
      bar.erase(bar.begin(), bar.end());
      dfs(i);
      sort(foo.begin(), foo.end());
      for (int j = 0; j < foo.size(); j++) bar.push_back(s[foo[j]]);
      sort(bar.begin(), bar.end());
      reverse(bar.begin(), bar.end());
      for (int j = 0; j < foo.size(); j++) ans[foo[j]] = bar[j];
    }
  }
  for (i = 1; i <= n; i++) cout << ans[i] << " ";
  cout << endl;
  return 0;
}
