#include <bits/stdc++.h>
using namespace std;
int ara[1000010];
vector<int> edge[1000010];
int ans[1000010];
int visited[1000010];
vector<int> pos;
vector<int> val;
bool cmp(int a, int b) { return a > b; }
void dfs(int root) {
  visited[root] = 1;
  pos.push_back(root);
  val.push_back(ara[root]);
  int sz = edge[root].size();
  for (int i = 0; i < sz; i++) {
    if (!visited[edge[root][i]]) dfs(edge[root][i]);
  }
}
int main() {
  int n, m;
  scanf("%d %d", &n, &m);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &ara[i]);
  }
  int a, b;
  for (int i = 1; i <= m; i++) {
    scanf("%d %d", &a, &b);
    edge[a].push_back(b);
    edge[b].push_back(a);
  }
  for (int i = 1; i <= n; i++) {
    if (!visited[i]) {
      val.clear();
      pos.clear();
      dfs(i);
      sort(val.begin(), val.end(), cmp);
      sort(pos.begin(), pos.end());
      int sz = val.size();
      for (int i = 0; i < sz; i++) {
        ans[pos[i]] = val[i];
      }
    }
  }
  for (int i = 1; i <= n; i++) {
    printf("%d", ans[i]);
    if (i == n)
      printf("\n");
    else
      printf(" ");
  }
  return 0;
}
