#include <bits/stdc++.h>
using namespace std;
const int INF = 2e9;
const long long INFL = 2e18;
const int N = 1e6, M = N;
int n, m, G = 0;
vector<int> adj[N + 5];
int gr[N + 5], p[N + 5];
vector<int> pth[N + 5];
vector<int> PTH_myEndlessLove;
int Pos[N + 5];
void dfs(int u) {
  gr[u] = G;
  PTH_myEndlessLove.push_back(u);
  for (int i = (0), _b = (((int)(adj[u]).size())); i < _b; ++i) {
    int v = adj[u][i];
    if (gr[v]) continue;
    dfs(v);
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cin >> n >> m;
  for (int i = (1), _b = (n); i <= _b; ++i) cin >> p[i];
  int u, v;
  while (m--) {
    cin >> u >> v;
    adj[p[u]].push_back(p[v]);
    adj[p[v]].push_back(p[u]);
  }
  for (int i = (1), _b = (n); i <= _b; ++i)
    if (!gr[p[i]]) {
      PTH_myEndlessLove.clear();
      ++G;
      dfs(p[i]);
      sort(PTH_myEndlessLove.begin(), PTH_myEndlessLove.end());
      reverse(PTH_myEndlessLove.begin(), PTH_myEndlessLove.end());
      pth[G] = PTH_myEndlessLove;
    }
  for (int i = (1), _b = (n); i <= _b; ++i) {
    cout << pth[gr[p[i]]][Pos[gr[p[i]]]] << ' ';
    ++Pos[gr[p[i]]];
  }
  cout << '\n';
  return 0;
}
