#include <bits/stdc++.h>
using namespace std;
vector<int> v[1000006];
vector<int> vv[1000006], brr[1000006];
int z, arr[1000006], vis[1000006];
void dfs(int u, int s) {
  vis[u] = 1;
  v[s].push_back(arr[u]);
  brr[s].push_back(u);
  for (int i = 0; i < vv[u].size(); i++) {
    int V = vv[u][i];
    if (!vis[V]) dfs(V, s);
  }
}
int main() {
  int n, m;
  scanf("%d %d", &n, &m);
  for (int i = 1; i <= n; i++) scanf("%d", &arr[i]);
  while (m--) {
    int x, y;
    scanf("%d %d", &x, &y);
    vv[x].push_back(y);
    vv[y].push_back(x);
  }
  for (int i = 1; i <= n; i++) {
    if (!vis[i]) dfs(i, ++z);
  }
  for (int i = 1; i <= z; i++) {
    sort(v[i].begin(), v[i].end());
    sort(brr[i].begin(), brr[i].end());
  }
  for (int i = 1; i <= z; i++) {
    for (int j = 0, k = v[i].size() - 1; j < brr[i].size(); j++, k--) {
      arr[brr[i][j]] = v[i][k];
    }
  }
  for (int i = 1; i <= n; i++) printf("%d ", arr[i]);
  printf("\n");
}
