#include <bits/stdc++.h>
using namespace std;
vector<list<int> > adjlist(2000000);
vector<int> key, value;
int visited[2000000], ans[2000000];
int ar[2000000];
bool compare(int a, int b) { return (a > b); }
void dfs(int i) {
  list<int>::iterator it;
  for (it = adjlist[i].begin(); it != adjlist[i].end(); it++) {
    if (!visited[*it]) {
      visited[*it] = 1;
      key.push_back(*it);
      value.push_back(ar[*it]);
      dfs(*it);
    }
  }
}
int main() {
  int n, m, i, a, b;
  cin >> n >> m;
  for (i = 1; i <= n; i++) {
    scanf("%d", &ar[i]);
  }
  for (i = 0; i < m; i++) {
    scanf("%d", &a);
    scanf("%d", &b);
    adjlist[a].push_back(b);
    adjlist[b].push_back(a);
  }
  for (i = 1; i <= n; i++) {
    if (!visited[i]) {
      visited[i] = 1;
      key.push_back(i);
      value.push_back(ar[i]);
      dfs(i);
      vector<int>::iterator it1, it2;
      sort(key.begin(), key.end());
      sort(value.begin(), value.end(), compare);
      it1 = key.begin();
      it2 = value.begin();
      while (it1 != key.end()) {
        ans[*it1] = (*it2);
        it1++;
        it2++;
      }
      key.clear();
      value.clear();
    }
  }
  for (i = 1; i <= n; i++) {
    printf("%d ", ans[i]);
  }
}
