#include <bits/stdc++.h>
using namespace std;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int n, m;
  cin >> n >> m;
  vector<vector<int>> ady(n + 1);
  vector<int> val(n + 1);
  for (int i = 1; i <= n; i++) {
    cin >> val[i];
  }
  for (int i = 0; i < m; i++) {
    int a, b;
    cin >> a >> b;
    ady[a].push_back(b);
    ady[b].push_back(a);
  }
  vector<int> comp(n + 1);
  queue<int> q;
  int actual = 1;
  for (int i = 1; i <= n; i++) {
    if (comp[i]) continue;
    comp[i] = actual;
    q.push(i);
    while (!q.empty()) {
      int curr = q.front();
      q.pop();
      for (auto a : ady[curr]) {
        if (comp[a]) continue;
        comp[a] = actual;
        q.push(a);
      }
    }
    actual++;
  }
  vector<vector<int>> nums(actual);
  for (int i = 1; i <= n; i++) {
    nums[comp[i]].push_back(val[i]);
  }
  for (int i = 1; i < actual; i++) {
    sort(nums[i].begin(), nums[i].end());
  }
  vector<int> ans(n + 1);
  for (int i = 1; i <= n; i++) {
    ans[i] = nums[comp[i]].back();
    nums[comp[i]].pop_back();
  }
  for (int i = 1; i <= n; i++) cout << ans[i] << ' ';
  cout << '\n';
}
