#include <bits/stdc++.h>
using namespace std;
int n, d;
bool used[1000006];
vector<int> g[1000006];
vector<int> perm, pos;
vector<int> arr(1000006);
vector<int> ans(1000006);
void dfs(int v) {
  if (used[v]) return;
  used[v] = true;
  pos.push_back(v);
  perm.push_back(arr[v]);
  for (auto to : g[v]) dfs(to);
}
int main() {
  cin >> n >> d;
  for (int i = 0; i < n; i++) {
    cin >> arr[i];
    arr[i]--;
  }
  vector<pair<int, int> > v(d);
  for (int i = 0; i < d; i++) {
    cin >> v[i].first >> v[i].second;
    v[i].first--;
    v[i].second--;
  }
  for (int i = 0; i < n; i++) {
    used[i] = false;
  }
  for (int i = 0; i < d; i++) {
    g[v[i].first].push_back(v[i].second);
    g[v[i].second].push_back(v[i].first);
  }
  int cnt = 0;
  for (int i = 0; i < n; i++) {
    if (!used[i]) {
      cnt++;
      pos.clear();
      perm.clear();
      dfs(i);
      sort(pos.begin(), pos.end());
      sort(perm.begin(), perm.end(), greater<int>());
      for (int j = 0; j < perm.size(); j++) ans[pos[j]] = perm[j];
    }
  }
  for (int i = 0; i < n; i++) {
    cout << ans[i] + 1 << " ";
  }
  return 0;
}
