#include <bits/stdc++.h>
using namespace std;
int get_parent(vector<int> &parents, int i) {
  if (parents[i] == i) {
    return i;
  }
  return parents[i] = get_parent(parents, parents[i]);
}
int main() {
  cin.sync_with_stdio(false);
  int n, m;
  cin >> n >> m;
  vector<int> a(n);
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
  }
  vector<int> parents(n);
  for (int i = 0; i < n; ++i) {
    parents[i] = i;
  }
  for (int i = 0; i < m; ++i) {
    int i1, i2;
    cin >> i1 >> i2;
    --i1, --i2;
    parents[get_parent(parents, i1)] = get_parent(parents, i2);
  }
  map<int, vector<int> > groups;
  for (int i = 0; i < n; ++i) {
    int root = get_parent(parents, i);
    if (groups.find(root) == groups.end()) {
      groups[root] = vector<int>();
    }
    groups[root].emplace_back(i);
  }
  for (int i = 0; i < groups.size(); ++i) {
    vector<int> group_elements;
    for (int ai : groups[i]) {
      group_elements.push_back(a[ai]);
    }
    sort(group_elements.rbegin(), group_elements.rend());
    int idx = 0;
    for (int ai : groups[i]) {
      a[ai] = group_elements[idx++];
    }
  }
  for (int ai : a) {
    cout << ai << " ";
  }
  return 0;
}
