#include <bits/stdc++.h>
using namespace std;
const long long MAXN = 1e6 + 10, inf = 1e18;
long long n, m, a[MAXN];
bool mark[MAXN];
vector<long long> tmp, adj[MAXN];
vector<vector<long long>> com;
void Read_input() {
  cin >> n >> m;
  for (long long i = 1; i <= n; i++) cin >> a[i];
  for (long long i = 1; i <= m; i++) {
    long long x, y;
    cin >> x >> y;
    adj[x].push_back(y);
    adj[y].push_back(x);
  }
}
void dfs(long long v) {
  tmp.push_back(v);
  mark[v] = true;
  for (auto i : adj[v])
    if (!mark[i]) dfs(i);
}
bool cmp(long long x, long long y) { return x > y; }
void Solve() {
  for (long long i = 1; i <= n; i++)
    if (!mark[i]) {
      dfs(i);
      com.push_back(tmp);
      tmp.clear();
    }
  for (long long i = 0; i < com.size(); i++) {
    sort(com[i].begin(), com[i].end());
    vector<long long> tm;
    for (auto j : com[i]) tm.push_back(a[j]);
    sort(tm.begin(), tm.end(), cmp);
    for (long long j = 0; j < com[i].size(); j++) a[com[i][j]] = tm[j];
  }
  for (long long i = 1; i <= n; i++) cout << a[i] << " ";
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  Read_input(), Solve();
  return 0;
}
