#include <bits/stdc++.h>
using namespace std;
int c[100005];
int a[1000005], vis[1000005], cnt;
vector<int> g[1000005], comp[1000005], vals[1000005];
void dfs(int node, int n) {
  int i, j, k, m, ans, val;
  vis[node] = 1;
  comp[cnt].push_back(node);
  vals[cnt].push_back(a[node]);
  j = g[node].size();
  for (i = 0; i < j; i++) {
    if (!vis[g[node][i]]) {
      dfs(g[node][i], n);
    }
  }
}
void dfs_getConnectedComponts(int n) {
  int i, j, k, m, ans, val, ii;
  cnt = 0;
  for (i = 0; i < n; i++) {
    if (!vis[i]) {
      dfs(i, n);
      sort(comp[cnt].begin(), comp[cnt].end());
      sort(vals[cnt].begin(), vals[cnt].end(), std::greater<int>());
      cnt++;
    }
  }
}
int main() {
  int i, j, k, m, n, ans, val, t;
  t = 1;
  while (t--) {
    cin >> (n);
    cin >> (m);
    for (i = 0; i < n; i++) {
      scanf("%d", &a[i]);
      g[i].clear();
      comp[i].clear();
      vals[i].clear();
      vis[i] = 0;
    }
    for (i = 0; i < m; i++) {
      scanf("%d %d", &j, &k);
      j--;
      k--;
      g[j].push_back(k);
      g[k].push_back(j);
    }
    dfs_getConnectedComponts(n);
    for (i = 0; i < cnt; i++) {
      k = comp[i].size();
      for (j = 0; j < k; j++) a[comp[i][j]] = vals[i][j];
    }
    for (i = 0; i < n; i++) {
      cout << a[i] << " ";
    }
    cout << endl;
  }
  return 0;
}
