#include <bits/stdc++.h>
using namespace std;
const int N = 1000001;
vector<int> g[N], comp;
int vis[N];
void dfs(int u) {
  vis[u] = true;
  comp.push_back(u);
  for (int i = 0; i < g[u].size(); ++i)
    if (!vis[g[u][i]]) dfs(g[u][i]);
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int n, m;
  cin >> n >> m;
  int a[n + 1];
  for (int i = 1; i <= n; ++i) cin >> a[i];
  for (int i = 1; i <= m; ++i) {
    int a, b;
    cin >> a >> b;
    g[a].push_back(b);
    g[b].push_back(a);
  }
  for (int i = 1; i <= n; ++i) {
    if (!vis[i]) {
      dfs(i);
      vector<int> z;
      for (int i = 0; i < comp.size(); ++i) z.push_back(a[comp[i]]);
      sort(z.begin(), z.end());
      reverse(z.begin(), z.end());
      sort(comp.begin(), comp.end());
      for (int i = 0; i < comp.size(); ++i) a[comp[i]] = z[i];
      comp.clear();
    }
  }
  for (int i = 1; i <= n; ++i) cout << a[i] << " ";
  cout << "\n";
}
